2012-07-03 83 views
0

我有一個包含CheckBox的GridView。更改GridView中RowCreated中CheckBox的屬性

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCreated="GridView1_RowCreated" 
    BackColor="#F1EFC5" CssClass="SearchEveryWhere_Table" Width="500px"> 
    <HeaderStyle BackColor="#5391DD" ForeColor="White" /> 
    <Columns> 
     <asp:BoundField DataField="CityCode" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" Visible="false"> 
      <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" CssClass="EntryCell hideElement" /> 
     </asp:BoundField> 
     <asp:BoundField DataField="Switch" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" Visible="false"> 
     <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" CssClass="EntryCell hideElement" /> 
     </asp:BoundField> 
     <asp:BoundField DataField="Page_Number" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" HeaderText="Page"> 
      <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" CssClass="EntryCell" /> 
     </asp:BoundField> 
     <asp:BoundField DataField="Name" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" HeaderText="Title"> 
      <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" CssClass="EntryCell" /> 
     </asp:BoundField> 
     <asp:TemplateField HeaderText="Access"> 
      <ItemTemplate> 
       <asp:CheckBox runat="server" AutoPostBack="true" OnCheckedChanged="chkStatus_OnCheckedChanged" Checked='<%# Convert.ToBoolean(Eval("Access")) %>' ID="chkStatus" ClientIDMode="Static" /> 
      </ItemTemplate> 
      <HeaderStyle HorizontalAlign="Center" /> 
      <ItemStyle HorizontalAlign="Center" /> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

我想這個CheckBox的變化ID的值,這樣的:

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) 
{ 
    try 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      sp_GetPageAccess08_New_Result CurrentREcord = (sp_GetPageAccess08_New_Result)e.Row.DataItem; 
      e.Row.Cells[4].FindControl("chkStatus").ID = "chkStatus_" + CurrentREcord.CityCode + "_" + CurrentREcord.Page_Number + "_" + CurrentREcord.Switch.ToString(); 
      if (CurrentREcord.Access == false) 
      { 
       e.Row.BackColor = System.Drawing.Color.FromArgb(252, 212, 243); 
      } 
     } 
    } 
    catch (Exception ex) 
    { 

    } 
} 

的問題是,當我想ID我得到chkStatuschkStatus_OnCheckedChanged

rotected void chkStatus_OnCheckedChanged(object sender, EventArgs e) 
{ 
    try 
    { 
     CheckBox chk = sender as CheckBox; 
     string ID = chk.ID; 

如何我可以更改的chkStatus?如果我無法將該字符串保存在可以獲得chkStatus_OnCheckedChanged的屬性中?

感謝

我的代碼
+0

我不會觸及ID屬性,因爲這是必需的(並在回發上重建)相反,我會使用複選框的屬性..檢查此鏈接使用attibutes。 http://www.codeproject.com/Tips/88324/Passing-command-arguments-to-a-checkbox – Nico

回答

1

修正

你OnRowDatabound在你的複選框選中/清除事件添加

((CheckBox)e.Row.Cells[4].FindControl("chkStatus")).Attributes.Add("MyData", string.Format("{0}_{1}_{2}", CurrentREcord.CityCode, CurrentREcord.Page_Number, CurrentREcord.Switch.ToString()); 

使用該工具來獲取數據

string data = chkStatus.Attributes["myData"]; 
+0

以及如何在'chkStatus_OnCheckedChanged1'中獲得'HiddenField'值?謝謝 – Arian

+0

這應該做到這一點 – JohnnBlade

0

你可以使用checkBox.Attributes.Add方法和yo將值存儲在自定義屬性中你也可以在CheckBox附近插入一個HiddenField控件,並將你想要的值存儲在它的Value屬性中(更好)。