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;
Related
I have a GridView in a ASP.NET web application, in which I have added image button in each row:
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="edit" runat="server" CommandArgument='<%# Bind("EmpID") %>' CommandName="edituser" ImageUrl="image/images.jpg"
ToolTip="Edit User Details"> </asp:ImageButton>
</ItemTemplate>
</asp:TemplateField>
Now how I can get the row data from gridview simply by clicking an edit image button in a row?
You have to change the CommandArgument with this one:
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>
Then:
protected void GridView1_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if (e.CommandName == "edituser") /*if you need this
{
// Retrieve the row index stored in the
// CommandArgument property.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the buttonfrom the Rows collection.
GridViewRow row = GridView1.Rows[index];
// Add code here now you have the specific row data
}
}
Bind all the columns in the label control respectively, and you can get value using findcontrol method at GridView1_SelectedIndexChanged event
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
Label _lblText = (Label)this.GridView1.SelectedRow.FindControl("UR_Desired_Label_ID");
this.TextBox3.Text = _lblText.Text ;
}
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'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
}
}
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"]);
how to get value from row in gridview using button to serverclick ?
this is my code :
<dx:ASPxGridView ID="gvwListApprover" runat="server" Width="460px">
<Columns>
<dx:GridViewDataTextColumn Caption="No" FieldName="Sequential" Width="20px"></dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn Caption="KodePosition" FieldName="KodePosition" Width="175px"></dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn Caption="Jabatan" FieldName="NamaPosition" Width="175px"></dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn Caption="Nama" FieldName="UserLogin" Width="265px"></dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn ReadOnly="True" Caption="Action">
<DataItemTemplate>
<input type="button" id="btnDelApp" onserverclick="btnDelApp_ServerClick('<%#Eval("KodePosition")%>');" value="Delete" runat="server" class="lookupstyle" />
</DataItemTemplate>
</dx:GridViewDataTextColumn>
</Columns>
</dx:ASPxGridView>
from onserverclick, i want get value to codebehind.
protected void btnDelApp_ServerClick(object sender, EventArgs e)
{
//how to get value from row in gridview event button on serverclick ?
}
please corrected and give me solution.
thanks
U Can go with the RowDataBoundEvent of the click.
DO Not Add any explict event for the control.
The GridView Automatically detects the event for the control clicked within it.
protected void grdClickDoubleClick_RowDataBound(object sender, GridViewRowEventArgs e) //formatted the c# code
{
GridViewRow row = e.Row;
if (row.RowType == DataControlRowType.DataRow)
{
//*******************************************************8
// code here. (This code given below)
//
}
}
for single click event assign
LinkButton _singleClickButton = row.FindControl("lnkBtnClk") asLinkButton;
string _jsSingleClick = ClientScript.GetPostBackClientHyperlink(_singleClickButton, "");
e.Row.Attributes.Add("onclick", _jsSingleClick);
for double click event assign
LinkButton _dblClickButton = row.FindControl("lnkBtnDblClk") asLinkButton;
string _jsDoubleClick = ClientScript.GetPostBackClientHyperlink(_dblClickButton, "");
e.Row.Attributes.Add("ondblclick", _jsDoubleClick);
This assigned events we can handle in row command method as below.
protected void grdClickDoubleClick_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
switch (e.CommandName)
{
case"clk": //"clk" is command name of linkButton Row click event handler
grdClickDoubleClick.SelectedIndex = row.RowIndex;
break;
case"dblClk"://"dblClk" is command name of linkButton Row Double click event handler.
row.BackColor = System.Drawing.Color.Pink;
break;
}
}
If we want to handle both click and double click then we have to change the above click handler a bit as below
LinkButton _singleClickButton = row.FindControl("lnkBtnClk") asLinkButton;
string _jsSingleClick = ClientScript.GetPostBackClientHyperlink(_singleClickButton, "");
///We make the script as below format.
//javascript:setTimeout("POSTBACK SCRIPT", 500)
_jsSingleClick = _jsSingleClick.Insert(11, "setTimeout(\"");
_jsSingleClick += "\", 500)";
e.Row.Attributes.Add("onclick", _jsSingleClick);
then we can handle both row click and double click.