Click here to Skip to main content
15,912,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ASP.NET
<asp:GridView ID="GrdRole" runat="server" CssClass="table table-striped table-bordered table-hover" EmptyDataText="No Records Found" DataKeyNames="RoleID"
             AllowPaging="true" AutoGenerateColumns="false" AutoGenerateDeleteButton="false" RowStyle-HorizontalAlign="Left" OnPageIndexChanging="GrdRole_PageIndexChanging"
             HeaderStyle-HorizontalAlign="Center" GridLines="None" PageSize="10" RowStyle-CssClass="gradeX" AlternatingRowStyle-CssClass="gradeA">
             <Columns>
                 <asp:TemplateField HeaderText="Select" ItemStyle-Width="10px">
                     <ItemTemplate>
                         <input type="checkbox" runat="server" id="emp" value='<%#Eval("EmployeeNo")%>' />
                     </ItemTemplate>
                 </asp:TemplateField>
                 <asp:TemplateField HeaderText="User Name" ItemStyle-Width="90%">
                     <ItemTemplate>
                         <label><%#DataBinder.Eval(Container.DataItem, "Name")%></label>
                     </ItemTemplate>
                 </asp:TemplateField>
             </Columns>
             <PagerStyle HorizontalAlign="Right" CssClass="pagination-ys" />
         </asp:GridView>


C#
protected void click(object sender, EventArgs e)
{
    string data = "";
    foreach (GridViewRow row in GrdRole.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            CheckBox chkRow = (row.Cells[0].FindControl("emp") as CheckBox);
            if (chkRow.Checked)
            {
                string storid = row.Cells[1].Text;
                data = data + storid + "<br>";
            }
        }
    }
    lblmsg.Text = data;
}

why lblmsg.text is empty?i did tick the checkbox

What I have tried:

any idea why it cant get my checkbox value?please help,thank you.
Posted
Updated 29-Jun-16 20:17pm

try this



C#
protected void Button1_Click(object sender, EventArgs e)
      {
          string data = "";
          foreach (GridViewRow row in GrdRole.Rows)
          {
              if (row.RowType == DataControlRowType.DataRow)
              {

                 CheckBox chkRow = (row.FindControl("emp") as CheckBox);
                  if (chkRow.Checked)
                  {
                      string storid = (row.FindControl("lbl") as Label).Text;
                      data = data + storid + "<br>";
                  }
              }
          }
          lblmsg.Text = data;
      }


ASP.NET
<columns>
                <asp:templatefield headertext="Select" itemstyle-width="10px"  >
                    <itemtemplate>
                        <asp:checkbox id="emp" runat="server" text="<%#Eval("EmployeeNo")%>" />
                    </itemtemplate>
                </asp:templatefield>
                <asp:templatefield headertext="User Name" itemstyle-width="90%"  >
                    <itemtemplate>
                        <asp:label id="lbl" runat="server" text="<%#Eval("Name")%>"></asp:label>
                    </itemtemplate>
                </asp:templatefield>
            </columns>
 
Share this answer
 
v2
Comments
KyLim0211 30-Jun-16 0:49am    
sorry i use your code but error : The server tag is not well formed.
<asp:checkbox id="emp" runat="server" text="<%#Eval("EmployeeNo")%>" />
Karthik_Mahalingam 30-Jun-16 0:51am    
<asp:checkbox id="emp" runat="server" text='<%#Eval("EmployeeNo")%>' />
KyLim0211 30-Jun-16 1:44am    
sorry your method not working too,it cant get the input
Karthik_Mahalingam 30-Jun-16 2:04am    
post your code.
Karthik_Mahalingam 30-Jun-16 2:12am    
this is what i have tried..

aspx code in Edit fiddle - JSFiddle[^]






protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack) return;
DataTable dt = new DataTable();
dt.Columns.Add("EmployeeNo");
dt.Columns.Add("Name");
dt.Columns.Add("RoleID");
dt.Rows.Add("Moto", "Nexus",1);
dt.Rows.Add("Apple", "Iphone",2);
dt.Rows.Add("Sony", "Vaio",3);
dt.Rows.Add("Samsung", "Edge",4);
GrdRole.DataSource = dt;
GrdRole.DataBind();


}

protected void Button1_Click(object sender, EventArgs e)
{
string data = "";
foreach (GridViewRow row in GrdRole.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{

CheckBox chkRow = (row.FindControl("emp") as CheckBox);
if (chkRow.Checked)
{
string storid = (row.FindControl("lbl") as Label).Text;
data = data + storid + "<br>";
}
}
}
lblmsg.Text = data;
}
ASP.NET
  <asp:gridview id="GrdRole" runat="server" cssclass="table table-striped table-bordered table-hover" emptydatatext="No Records Found" datakeynames="RoleID" xmlns:asp="#unknown">
                AllowPaging="true" AutoGenerateColumns="false" AutoGenerateDeleteButton="false" RowStyle-HorizontalAlign="Left" OnPageIndexChanging="GrdRole_PageIndexChanging"
                HeaderStyle-HorizontalAlign="Center" GridLines="None" PageSize="10" RowStyle-CssClass="gradeX" AlternatingRowStyle-CssClass="gradeA">
                <columns>
                    <asp:templatefield headertext="Select" itemstyle-width="10px">
                        <itemtemplate>
                            <input type="checkbox" runat="server" id="emp" value='<%#Eval("EmployeeNo")%>' />
                        </itemtemplate>
                    </asp:templatefield>
                    <asp:templatefield headertext="User Name" itemstyle-width="90%">
                        <itemtemplate>
                            <label><%#DataBinder.Eval(Container.DataItem, "Name")%></label>
                        </itemtemplate>
                    </asp:templatefield>
                </columns>
                <pagerstyle horizontalalign="Right" cssclass="pagination-ys" />
            </asp:gridview>
<td>
            <asp:button id="submit_button" text="Update" cssclass="btn btn-success" runat="server" onclick="UpdateBtm" xmlns:asp="#unknown" />
        </td>


my c#
C#
protected void UpdateBtm(object sender, EventArgs e)
   {
       foreach (GridViewRow row in GrdRole.Rows)
       {
           if (row.RowType == DataControlRowType.DataRow)
           {
               CheckBox chkRow = (row.Cells[0].FindControl("emp") as CheckBox);
               if (chkRow.Checked)
               {
                   string storid = row.Cells[0].Text;
                   ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + storid + "');", true);
               }
           }
       }

   }
 
Share this answer
 
Comments
Karthik_Mahalingam 30-Jun-16 2:28am    
dont use solution widget to post comments
delete it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900