Reputation: 203
i have this gridview and im trying to print out the MMBR_PROM_ID of whichever column is checked by the user.
(Default.apsx)
Welcome to ASP.NET!
</h2>
<div style="width: 700px; height: 370px; overflow: auto; float: left;">
<asp:GridView ID="GridView1" runat="server" HeaderStyle-CssClass="headerValue"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:TemplateField HeaderText="Generate">
<ItemTemplate>
<asp:CheckBox ID="grdViewCheck" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Generate" />
</asp:Content>
(Default.aspx.cs)
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FrontOffEntities tmpdb = new FrontOffEntities();
List<MMBR_PROM> newListMMBR_Prom = tmpdb.MMBR_PROM.ToList();
GridView1.DataSource = newListMMBR_Prom;
GridView1.DataBind();
}
}
so my goal is when i press generate i want to be able to print out as string all the MMBR_PROM_ID which is checked by the user. im kinda new to aspnet so im having a hard time coping up with the syntax
Upvotes: 2
Views: 7894
Reputation: 469
Per your mentioned requirements you can try the below given code to get the values of the MMBR_PROM_ID from Gridview1 on Generate Button Click.
//For every row in the grid
foreach (GridViewRow r in GridView1.Rows)
{
//Find the checkbox in the current row being pointed named as grdViewCheck
CheckBox chk = (CheckBox)r.FindControl("grdViewCheck");
//Print the value in the reponse for the cells[1] which is MMBR_PROM_ID
if (chk!=null && chk.Checked)
{
Response.Write(r.Cells[1].Text);
}
}
Here, cells[1] refers to the Cell Index of the Specific row, in your case it is MMBR_PROM_ID, which you want to print. Hope this helps!
If you are looking for a comma seperated value of MMBR_PROM_ID, the below mentioned code will work for you.
//Declaration of string variable
string str="";
//For every row in the grid
foreach (GridViewRow r in GridView1.Rows)
{
//Find the checkbox in the current row being pointed named as grdViewCheck
CheckBox chk = (CheckBox)r.FindControl("grdViewCheck");
//Print the value in the reponse for the cells[1] which is MMBR_PROM_ID
if (chk!=null && chk.Checked)
{
Response.Write(r.Cells[1].Text);
//appending the text in the string variable with a comma
str = str + r.Cells[1].Text + ", ";
}
}
//Printing the comma seperated value for cells[1] which is MMBR_PROM_ID
Response.Write(str);
Upvotes: 2