看来CheckBoxField字段将不会接受一个ID属性,所以我不能直接调用隐藏文件中的code中的组成部分。
It seems CheckBoxField won't accept an ID property, so I can't directly call the component in the code behind file.
<asp:DetailsView ID="dv" runat="server" AutoGenerateRows="False"
DataKeyNames="ID" DataSourceID="ds" DefaultMode="Insert"
OnItemInserting="dv_ItemInserting" OnItemInserted="dv_ItemInserted"
OnItemUpdated="dv_ItemUpdated" OnItemCommand="dv_ItemCommand"
EnableModelValidation="True">
<Fields>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="false" ReadOnly="True" SortExpression="ID" />
<asp:CheckBoxField ID="chkMidmarket" DataField="Midmarket_Flag" HeaderText="Midmarket" SortExpression="Midmarket_Flag" runat="server" />
...
</Fields>
</asp:DetailsView>
如何将我从后面文件中的code选中复选框?
How would I check the checkbox from the code behind file?
在syour设计视图去GridView的编辑栏,选择列,然后单击转换为模板字段
In syour design view go to gridview edit columns and select the column and click convert to template field
<asp:TemplateField HeaderText="Midmarket" SortExpression="Midmarket_Flag">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged ="CheckBox_CheckedChanged"
Checked='<%# Bind("Midmarket_Flag") %>' Enabled="false" />
</ItemTemplate>
</asp:TemplateField>
现在,您可以添加click事件如上,你的事件类似下面
Now you can add click event as above and your event something like below
protected void CheckBox_CheckedChanged(object sender, EventArgs e)
{
try
{
CheckBox cb = sender as CheckBox;
GridViewRow gr = cb.Parent.Parent as GridViewRow;
string key = GridView1.DataKeys[gr.DataItemIndex].Value.ToString();
}
catch (Exception exc)
{
}
}