I'm using a checkbox column in a gridview which is populated from a SQL database. A button below the gridview should retrieve the data of the rows whose checkboxe's have been checked. When I loop over all the rows checkboxe's, none of them has checked==true even though I have checked them all before clicking the button. Here is the ASP.NET code:
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkRow" runat="server" />
</ItemTemplate>
</asp:TemplateField>
and here is the button's action that loops over all the rows' checkboxes:
protected void GetSelectedRecords(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox);
if (chkRow != null && chkRow.Checked) //chkRow.Checked is always "false"
{
string name = row.Cells[2].Text;
}
}
}
}
Thanks a lot in advance. I'd greatly appreciate any help.
If you are binding your grid in Page_Load, Make sure you are not binding your grid outside of if(!IsPostBack){}. Otherwise you will loose the checkboxe's on each postback and therefore losing the status of checkboxe's.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
//Bind Your Grid Here
}
}
Related
I have used grid view in my ASP.NET application.
<asp:GridView ID="grdView" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkbox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Jurisdiction">
<ItemTemplate>
<asp:Label ID="lblJurisdiction" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="License Number">
<ItemTemplate>
<asp:TextBox ID="txtLicenseNumber" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
in cs file
protected void btnSave_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in grdView.Rows)
{
for (int i = 0; i < grdView.Columns.Count; i++)
{
String cellText = row.Cells[i].Text;
}
}
}
Note that the above grid will be populated by data. Now I need to get data from already populated gridview. The above code is not working. Also I need to retrieve from labels, textboxes, checkboxes values inside grid. Please help !!!
You can use FindControl method to retrieve the control's data:-
protected void btnSave_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in grdView.Rows)
{
CheckBox chkbox = row.FindControl("chkbox") as CheckBox;
Label lblJurisdiction = row.FindControl("lblJurisdiction") as Label;
..and so on
//Finally retrieve the data like your normal control
string labelText = lblJurisdiction.Text;
}
}
Use GridViewRow.FindControl method.
foreach (GridViewRow row in grdView.Rows)
{
// return you the check-box control from current row
var chkbox = row.FindControl("chkbox");
}
Check whether the row type is DataControlRowType.DataRow.
protected void btnSave_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in grdView.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < grdView.Columns.Count; i++)
{
String cellText = row.Cells[i].Text;
}
}
}
}
To get the TextBox, CheckBox value from the grid use this,
string TextBoxvalue = ((TextBox)GridViewID.Rows[i].FindControl("TextBoxName")).Text;
string CheckBoxvalue = ((CheckBox)GridViewID.Rows[i].FindControl("CheckBoxName")).Text;
In your code .cs file, you are missing to check only for datarow. As it will check all 3 places:-
1. Header
2. Body
3. Footer
Might any one place, any exception can occur.
Please add one more if condtion just after the for loop, as below.
if (row.RowType == DataControlRowType.DataRow)
{
Hope this post will help's you :).
Use
cells[i].EditedFormatedValue
I am trying to update a user in a different page with textboxes. But what I'm trying to do is select the user based on a Gridview.
So on the first page You see a gridview with a user list and checkboxes. You select a user with a check box and then hit the Edit User button. Then it goes to a new page which allows you to edit the user in textboxes but populates the data because of the ID or another unique column. How would I do this??
Here is the code I have so far:
protected void Button2_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvrow in GridView1.Rows)
{
CheckBox CheckBox1 = (CheckBox)gvrow.FindControl("chkSelect");
if (CheckBox1.Checked)
{
//Go to new page to edit user
}
else
{
//Do nothing if not checked
}
}
}
I assume that your checkbox is inside a <asp:TemplateField>.
So, in a <asp:TemplateField> you could add a <asp:HiddenField /> to store the ID of the user. Then you can get the ID by getting the value of the <asp:HiddenField />
ASPX markup:
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
<asp:HiddenField ID="hfdUserID" runat="server" Value='<%#Eval("id") %>' />
</ItemTemplate>
</asp:TemplateField>
Code behind:
protected void Button2_Click(object sender, EventArgs e)
{
int userid = 0;
foreach (GridViewRow gvrow in GridView1.Rows)
{
CheckBox CheckBox1 = (CheckBox)gvrow.FindControl("chkSelect");
if (CheckBox1.Checked)
{
userid = Convert.ToInt32(((HiddenField)gvrow.FindControl("hfdUserID")).Value);
Response.Redirect("useredit.aspx?id=" + userid.ToString());
}
}
}
Edit: To get the user ID to the new page you can use the following and then use it accordingly
int userid = Convert.ToInt32(Request.QueryString["id"]);
before I asked this I did some checking around to make sure that this doesn't turn out to be a duplicate and ways to get the row values from a row that has a checkbox in its template field...but I can't seem to get it working...So far I have tried
protected void Page_Load(object sender, EventArgs e)
{
Entities NW = new Entities();
var customers =
from c in NW.Customers
where (c.ContactName.StartsWith("Ma") || c.ContactName.StartsWith("An")
|| c.ContactName.StartsWith("T")
|| c.ContactName.StartsWith("V")
)
orderby c.ContactName ascending
select new
{
c.CustomerID,
c.ContactName,
c.CompanyName,
c.City
};
gv1.DataSource = customers.ToList();
gv1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in gv1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkRow = (row.Cells[0].FindControl("CB") as CheckBox);
if (chkRow.Checked)
{
Label1.Text = row.Cells[2].Text;
}
}
}
}
I have stepped through the button click event and when it gets to
if (chkRow.Checked)
its showing as null and skips over it..
my markup is
<asp:GridView ID="gv1" runat="server">
<HeaderStyle BackColor="SkyBlue" />
<AlternatingRowStyle BackColor="Yellow" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CB" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
when I look at the source after I run, the checkboxes are all named differently than what I gave them the ID of "CB", attached is the pic of the source when its running
I am not sure what I am doing wrong with this
Your GridView should have more columns, because you are referencing Cell[2] in your code.
You might try to use row object to look for your control:
CheckBox chkRow = (row.FindControl("CB") as CheckBox);
My Button1 is inside a Panel, I want to access the rowindex so that I will hide that Imagebutton. But when I enter debug mode, the GridView1.SelectedIndex has a null value. pleaase help!
protected void Button1_Click1(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
if (row.RowIndex == Convert.ToInt32(GridView1.SelectedIndex))
{
ImageButton StopButton = (ImageButton)row.FindControl("stopImageButton");
ImageButton StartButton = (ImageButton)row.FindControl("startImageButton");
StopButton.Visible = true;
StartButton.Visible = false;
}
}
}
this.StopTimeNotesPanel_ModalPopupExtender.Hide();
}
You said that Button is inside a panel. To be able to handle GridView events effectively, use a button inside Gridview itself.
OR if you still want to use button in panel, then,
1.) First Add a select button inside Gridview. Select a row using the select button and
2.) click the button in Panel.
GridView.SelectedIndex is set only when you have selected a row of Grid view. Two ways are possible:
1.) Set AutoGenerateSelectButton property to true.
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
autogenerateselectbutton="True"
runat="server">
2.) Add yourself a buttonField inside section of gridView as:
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="false"
runat="server">
<columns>
<asp:buttonfield buttontype="Button"
commandname="Select"
headertext="Select Customer"
text="Select"/>
<asp:boundfield datafield="CompanyName"
headertext="Company Name"/>
</columns>
</asp:gridview>
Now after a row is selected, two events of GridView will be fired:
selectedindexchanging & selectedindexchanged.
Only when required, do this step below to get SelectedRow in SelctedIndexChangedEvent
void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e)
{
// Get the currently selected row using the SelectedRow property.
GridViewRow row = CustomersGridView.SelectedRow;
MessageLabel.Text = "You selected " + row.Cells[2].Text; // just for Display
}
Now, inside your button click event , get the selected index:
protected void Button1_Click1(object sender, EventArgs e)
{
int i = CustomersGridView.SelectedIndex;
}
Code for for your foreach loop:
ImageButton StopButton = (ImageButton)row.FindControl("stopImageButton");
ImageButton StartButton = (ImageButton)row.FindControl("startImageButton");
StopButton.Visible = true;
StartButton.Visible = false;
I have a user control named ActivityGrid which takes a list of object as a parameter in it's constructor.
public ActivityGrid(List<clsActivityRow> ActivityData)
{
bindData(ActivityData);
}
I need to bind this user control in a gridView called parentGrid, so I used Templatefield.
<asp:GridView ID="GridViewParent" runat="server" AutoGenerateColumns ="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<tr>
<td colspan="100%">
<uc1:ActivityGrid ID="ActivityGrid1" runat="server" />
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
How do I pass the list of object in this user control while it is
bound inside a gridview?
I have the list of object List<clsActivityRow> ActivityData ready in the code behind.
You need to find the ActivityGrid1 object in every row of GridViewParent in its RowDataBound event and assign datasource.
protected void GridViewParent_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ActivityGrid ActivityGrid1= (ActivityGrid )e.Row.FindControl("ActivityGrid1");
if (ActivityGrid1 != null)
{
ActivityGrid1.DataSource = SomeMethodToReturnDataSource();
ActivityGrid1.DataBind();
}
}
}
or if your control has grid view i.e ActivityGrid1 has gridView1 then you can find gridView1 in ActivityGrid1
protected void GridViewParent_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ActivityGrid ActivityGrid1= (ActivityGrid )e.Row.FindControl("ActivityGrid1");
if (ActivityGrid1 != null)
{
GridView gridView1 = (ActivityGrid )ActivityGrid1 .FindControl("gridView1");
gridView1.DataSource = SomeMethodToReturnDataSource();
gridView1.DataBind();
}
}
}
You can do this by creating a public Property in your UserControl and In this public property set block you can call bindData method.
Pass the object List ActiveData for this usercontrol, in GridView RowDataBound Event.