[英]Controlling Checkboxes inside a Gridview Row in ASP.NET
不太確定如何處理此問題,但是這里...
我有一個gridview,每行有兩個復選框,下面是項目模板的示例:
<ItemTemplate>
<asp:CheckBox ID="MasterCheckbox" runat="server"/>
<asp:CheckBox ID="ChildCheckbox" runat="server" />
</ItemTemplate>
我希望ChildCheckbox的'enabled'屬性由MasterCheckbox的'Checked'屬性控制...換句話說,ChildCheckbox僅在MasterCheckbox被選中時才啟用。
我知道我將需要在MasterCheckbox控件上附加一個處理程序,以調用一些javascript在客戶端執行必需的操作-這可能會在row_databound()方法中完成嗎?
我不太清楚要使其正常工作所需的javascript,因此歡迎您提供任何提示。
謝謝
達爾
首先,您不需要回答自己的問題,您可以在第一個問題中添加評論。
由於您使用的是GridView,我認為您正在為MasterCheckBox綁定某些內容,因此可以說它是dataTable中的布爾值。 例如,這是名稱為IsMasterChecked的行合並列
您可以處理通過綁定自定義表達式來啟用另一個
<ItemTemplate>
<asp:CheckBox ID="MasterCheckbox" runat="server" />
<asp:CheckBox ID="ChildCheckbox" runat="server" Enabled='<%# Convert.ToBoolean(Eval("IsMasterChecked")) %>'/>
</ItemTemplate>
要么
<ItemTemplate>
<asp:CheckBox ID="MasterCheckbox" runat="server" />
<asp:CheckBox ID="ChildCheckbox" runat="server" Enabled='<%# Convert.ToBoolean(Eval("IsMasterChecked")) ? "true" : "false" %>'/>
</ItemTemplate>
希望這可以幫助。
我想,您要做的就是遵循以下內容...
<asp:TemplateField HeaderText="Checkbox">
<ItemTemplate>
<asp:CheckBox ID="MasterCheckbox" runat="server" AutoPostBack="true" OnCheckedChanged="checkGridViewChkBox" />
</ItemTemplate>
</asp:TemplateField>
后面有以下代碼。
CheckBox MasterCheckbox;
CheckBox ChildCheckbox;
private void checkGridViewChkBox()
{
int i;
int x = GridView1.Rows.Count;
for (i = 0; i < x; i++) //loop through rows
{
findControls(i);
if (MasterCheckbox.Checked)
{
ChildCheckbox.Enabled = true;
}else{
ChildCheckbox.Enabled = false;
}
}
}
private void findControls(int i)
{
MasterCheckbox = (CheckBox)(GridView1.Rows[i].FindControl("MasterCheckbox"));
ChildCheckbox = (CheckBox)(GridView1.Rows[i].FindControl("ChildCheckbox"));
}
它效率不是很高,但是可以。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.