give the ID as a custom attribute to checkbox in a grid for manual update

心已入冬 提交于 2019-12-24 20:19:26

问题


I like to update just the value of my checkbox in a asp grid.

so i thought i bind the id to the checkbox ( but how?) and fire an update in code behind by clicking the checkbox.

but how can I get the ID, and where I have to bind this id to get it in code behind in the event?

Thanks


回答1:


Here is what I've managed to do. (Be aware there should be an easier way to do this. I'm very new to ASP.NET)

Here you have a TemplateField in a GridView. Inside it there is an UpdatePanel and the CheckBox is inside it. This is done to make the checking of the TextBox do post in the backgroung (ajax). You might not need it (UpdatePanel) at all.

<asp:TemplateField HeaderText="Private" SortExpression="IsPrivate">
  <ItemTemplate>
    <asp:UpdatePanel ID="upIsPrivate" runat="server" UpdateMode="Always" ChildrenAsTriggers="true">
      <ContentTemplate>
        <asp:CheckBox ID="chkIsPrivate" runat="server" OnCheckedChanged="chkIsPrivate_CheckedChanged" AutoPostBack="true" />
      </ContentTemplate>
    </asp:UpdatePanel>
  </ItemTemplate>
</asp:TemplateField>

And this is the method that handles this. Notice that I get the Id from the GridViewRow that contains the CheckBox:

GridViewRow row = (GridViewRow)((CheckBox)sender).Parent.Parent.Parent.Parent;

protected void chkIsPrivate_CheckedChanged(object sender, EventArgs e)
{
  if (editMode)
  {
    GridViewRow row = (GridViewRow)((CheckBox)sender).Parent.Parent.Parent.Parent;
    Int32 id = (Int32)uxPhoneCallList.DataKeys[row.RowIndex]["Id"];
    CheckBox isPrivate = (CheckBox)row.FindControl("chkIsPrivate");

    PhoneCall phoneCall = PhoneCallManager.GetById(id);
    ...
  }
}


来源:https://stackoverflow.com/questions/1063602/give-the-id-as-a-custom-attribute-to-checkbox-in-a-grid-for-manual-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!