Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 837 Vote(s) - 3.45 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get row data by clicking a button in a row in an ASP.NET gridview

#1
I have a `GridView` in a ASP.NET web application, in which I have added two buttons in each row:

<!-- language: lang-html -->

<ItemTemplate>
<asp:Button ID="btnEdit" Text="Edit" runat="server" />
<asp:Button ID="btnDelete" Text="Delete" runat="server"/>
</ItemTemplate>

Now how I can get the row data from gridview simply by clicking an edit button in a row?
Reply

#2
<asp:Button ID="btnEdit" Text="Edit" runat="server" OnClick="btnEdit_Click" CssClass="CoolButtons"/>


protected void btnEdit_Click(object sender, EventArgs e)
{
Button btnEdit = (Button)sender;
GridViewRow Grow = (GridViewRow)btnEdit.NamingContainer;
TextBox txtledName = (TextBox)Grow.FindControl("txtAccountName");
HyperLink HplnkDr = (HyperLink)Grow.FindControl("HplnkDr");
TextBox txtnarration = (TextBox)Grow.FindControl("txtnarration");
//Get the gridview Row Details
}


And Same As for Delete button
Reply

#3
Place the `commandName` in .aspx page

<asp:Button ID="btnDelete" Text="Delete" runat="server" CssClass="CoolButtons" CommandName="DeleteData"/>

Subscribe the `rowCommand` event for the grid and you can try like this,

protected void grdBillingdata_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "DeleteData")
{
GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
HiddenField hdnDataId = (HiddenField)row.FindControl("hdnDataId");
}
}
Reply

#4
You can also use button click event like this:

<!-- language: lang-html -->

<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Button"
OnClick="MyButtonClick" />
</ItemTemplate>
</asp:TemplateField>

<!-- language: lang-cs -->

protected void MyButtonClick(object sender, System.EventArgs e)
{
//Get the button that raised the event
Button btn = (Button)sender;

//Get the row that contains this button
GridViewRow gvr = (GridViewRow)btn.NamingContainer;
}

### OR

You can do like this to get data:

<!-- language: lang-cs -->

void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{

// If multiple ButtonField column fields are used, use the
// CommandName property to determine which button was clicked.
if(e.CommandName=="Select")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);

// Get the last name of the selected author from the appropriate
// cell in the GridView control.
GridViewRow selectedRow = CustomersGridView.Rows[index];
}
}

and Button in gridview should have command like this and handle rowcommand event:

<!-- language: lang-html -->

<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="false"
onrowcommand="CustomersGridView_RowCommand"
runat="server">

<columns>
<asp:buttonfield buttontype="Button"
commandname="Select"
headertext="Select Customer"
text="Select"/>
</columns>
</asp:gridview>


[**Check full example on MSDN**](

[To see links please register here]

)
Reply

#5
Is there any specific reason you would want your buttons in an item template.You can alternatively do it the following way , there by giving you the full power of the grid row editing event.You are also given a bonus of wiring easily the cancel and delete functionality.

**Mark up**

<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:ImageButton ID="EditImageButton" runat="server" CommandName="Edit"
ImageUrl="~/images/Edit.png" Style="height: 16px" ToolTip="Edit"
CausesValidation="False" />

</ItemTemplate>

<EditItemTemplate>

<asp:LinkButton ID="btnUpdate" runat="server" CommandName="Update"
Text="Update" Visible="true" ImageUrl="~/images/saveHS.png"
/>
<asp:LinkButton ID="btnCancel" runat="server" CommandName="Cancel"
ImageUrl="~/images/Edit_UndoHS.png" />

<asp:LinkButton ID="btnDelete" runat="server" CommandName="Delete"
ImageUrl="~/images/delete.png" />

</EditItemTemplate>


<ControlStyle BackColor="Transparent" BorderStyle="None" />
<FooterStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>

**Code behind**

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{

GridView1.EditIndex = e.NewEditIndex;
GridView1.DataBind();

TextBox txtledName = (TextBox) GridView1.Rows[e.NewEditIndex].FindControl("txtAccountName");

//then do something with the retrieved textbox's text.

}
Reply

#6

<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Button"
OnClick="MyButtonClick" />
</ItemTemplate>

and your method

protected void MyButtonClick(object sender, System.EventArgs e)
{
//Get the button that raised the event
Button btn = (Button)sender;

//Get the row that contains this button
GridViewRow gvr = (GridViewRow)btn.NamingContainer;
}
Reply

#7
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" ID="LnKB" Text='edit' OnClick="LnKB_Click" >
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

protected void LnKB_Click(object sender, System.EventArgs e)
{
LinkButton lb = sender as LinkButton;

GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
int x = clickedRow.RowIndex;
int id = Convert.ToInt32(yourgridviewname.Rows[x].Cells[0].Text);
lbl.Text = yourgridviewname.Rows[x].Cells[2].Text;
}
Reply

#8
protected void btnS10_click(object sender, EventArgs e)
{
foreach (GridViewRow row in Grd.Rows)
{
CheckBox chk_Single = (CheckBox)row.FindControl("ChkSendOne");
if (row.RowType == DataControlRowType.DataRow)
{
string id = (row.Cells[0].FindControl("lblSNo") as Label).Text;
if (Convert.ToInt32(id) <= 10)
{

chk_Single.Checked = true;
if (chk_Single.Checked == true)
{
lblSelectedRecord.InnerText = (Convert.ToInt32(lblSelectedRecord.InnerText) + 1).ToString();
}
}
}
}
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through