Check a CheckBoxField from code behind file

后端 未结 2 1623
Happy的楠姐
Happy的楠姐 2021-01-24 22:33

It seems CheckBoxField won\'t accept an ID property, so I can\'t directly call the component in the code behind file.



        
相关标签:
2条回答
  • 2021-01-24 23:06

    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>
    

    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)
        {
        }
    }
    
    0 讨论(0)
  • 2021-01-24 23:10

    use

    <asp:CheckBox ID="youid" runat="server" />
    

    this way you can access it from your code behind

    youid.Checked = true;
    
    0 讨论(0)
提交回复
热议问题