3

テンプレートフィールドとしてチェックボックスを含むgridviewがあります。私は一生懸命努力しましたが、それでも目的の結果を得ることができません。つまり、チェックボックスがオンになっている場合は、アクション1を実行するよりも、アクション2を実行するよりも、アクション2を実行するたびに実行できます。以下は私のコードです。あなたの側からの助けはほとんど必要ありません。

グリッドビューコード:

<asp:GridView ID="final" runat="server" AutoGenerateColumns="False";>
        <Columns>
<asp:BoundField DataField="name" HeaderText="Employee Name" SortExpression="date" />
<asp:BoundField DataField="ldate" HeaderText="Date Of Leave" SortExpression="ldate"
                   />
<asp:TemplateField HeaderText="Half/Full">
   <ItemTemplate>
            <asp:RadioButtonList ID="RadioButtonList1" runat="server">
                            <asp:ListItem Enabled="true" Value="Half">Half</asp:ListItem>
                            <asp:ListItem Enabled="true" Value="Full">Full</asp:ListItem>
           </asp:RadioButtonList>
   </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Approve">
    <ItemTemplate>
     <asp:CheckBox ID="CheckBox1" runat="server" />
    </ItemTemplate>
</asp:TemplateField>
        </Columns>
</asp:GridView>

ラジオとチェックボックスをチェックするために作成したコード:

DataTable dtable = new DataTable();
dtable.Columns.Add(new DataColumn("Date", typeof(DateTime)));
dtable.Columns.Add(new DataColumn("Half/Full", typeof(float)));
dtable.Columns.Add(new DataColumn("Status", typeof(string)));
Session["dt"] = dtable;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["leave"].ConnectionString;
conn.Open();
foreach (GridViewRow gvrow in final.Rows)
{
     dtable=(DataTable)Session["dt"];
     CheckBox chk = (CheckBox)gvrow.FindControl("CheckBox1");
     if (chk != null & chk.Checked)
     {
          RadioButtonList rButton = (RadioButtonList)gvrow.FindControl("RadioButtonList1");
          if (rButton.SelectedValue == "Half")
          {
               //perform action-1
          }
          else
          {
              //perform action-1
          }
     }
  else
     {
          perform action-2
     }
}

それが最後に入るたびに...なぜですか?

4

3 に答える 3

4

ifステートメントの条件を組み合わせるには&&、ビット演算子の代わりに論理演算子と演算子を使用します。&

変化する

if (chk != null & chk.Checked)

if (chk != null && chk.Checked)

OPのコメントに基づいて編集

ポストバックでバインドされないようにグリッドをバインドすることを確認する必要があります。

if(!Page.IsPostBack)
{
       //bind here
} 
于 2013-03-25T04:55:53.603 に答える
0

ページの読み込み時にグリッドビューを再度バインドしていないことを確認してください。バインドしている場合は、IsPostbackチェックを適用する必要があります。

于 2013-03-25T05:52:50.267 に答える
0

これを試して

<asp:CheckBox ID="CheckBox1" runat="server"  OnCheckedChanged="CheckBox1_CheckedChanged"  AutoPostBack="true"/>

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
    CheckBox chk = (CheckBox)sender;
    GridViewRow gr = (GridViewRow)chk.Parent.Parent;
    RadioButtonList RadioButtonList1 = (RadioButtonList)gr.FindControl("RadioButtonList1");
    if (RadioButtonList1 != null)
    {
        RadioButtonList1.Items.FindByText("Full").Selected = true;
    }       
}
于 2013-03-25T11:09:14.420 に答える