C# .NET - How to find control of gridview in button event

Asked By abinav shankar on 18-Jan-12 01:05 AM
Hi

I want to find control of the gridview itemtemplate I used the code below and i am getting the value but because of using the
for loop i am getting only the last value for instance if i need the value of the first row I am getting only the value of second row i am pasting my code here pl help me how to do it

<asp:TemplateField HeaderText="Associate ID">
        <ItemTemplate>
        <asp:LinkButton ID="lnkAssId" Text='<%# Eval("associate_id") %>' runat="server" 
          onclick="lnkAssId_Click">LinkButton</asp:LinkButton>
        </ItemTemplate>
      </asp:TemplateField>
  
  
protected void lnkAssId_Click(object sender, EventArgs e)
  {
  for (int i = 0; i < grdApproval.Rows.Count; i++)
  {
    LinkButton lblAssId = (LinkButton)grdApproval.Rows[i].FindControl("lnkAssId");
    Session["AssId"] = lblAssId.Text;
  }
  
  }
D Company replied to abinav shankar on 18-Jan-12 01:10 AM
Hello Abinav,

You can try write this in grid row databound evens

for e.g

void grvValidCourses_RowDataBound(object sender, GridViewRowEventArgs e)
 {  
if (e.Row.RowType == DataControlRowType.DataRow)  
{  
 
// here fetch each row and call some function
   }
 }

Hope this will help you

Regards
D
Sandeep Mittal replied to abinav shankar on 18-Jan-12 01:24 AM
protected void lnkAssId_Click(object sender, EventArgs e)
{
  Session["AssId"] = ((LinkButton)sender).Text;     
}
dipa ahuja replied to abinav shankar on 18-Jan-12 03:29 AM
void bind()
{
  foreach (GridViewRow row in GridView1.Rows)
  {
    string name = ((Label)row.FindControl("label1")).Text.ToString();
    string gender = ((DropDownList)row.FindControl("dropdown1")).SelectedValue.ToString();
    string contact = ((Label)row.FindControl("lblcontact")).Text.ToString();
 
    //code to write
  }
}
 
help
link button inside gridvieww not firing javascript will u pls explain detailly ? show ur code to solve ur issue. . . To identify linkbutton in GridView Use FindControl to find the linkbutton LinkButton LnkAdd = (LinkButton)e.Row FindControl("LnkAdd"); Now add JavaScript OnClick Function LnkAdd .Attributes.Add( "onclick" , "show();" ); See the JavaScript Function
public static void ExportDataSetToExcel( DataSet ds , string code which will be written in aspx.cs protected void lnkCustomerID_Click(object sender, EventArgs e) { LinkButton link = (LinkButton)sender; GridViewRow gv = (GridViewRow)(link.Parent.Parent); LinkButton CustomerID = (LinkButton)gv.FindControl("lnkCustomerID"); Session["customerid"] = CustomerID.Text; Response.Redirect("customer_detail.aspx"); } and here's the code of gridview's link button for your .aspx page <asp:GridView ID = "GridView1" runat = "server" Width = "100%
Hi All how we findcontrol on pageload of linkbutton in gridview?? not row databound thx. . . . . . . If you want to find the label from grid view in rows and and try to find the label control like for(int i = 0;i<gridview.Rows.Count;i++) { Label lbl = gridview.Row[i].FindControl("lblTest") as Label; if(lbl! = null) { lbl.Text = "Set your value here"; } } hi, in that case you need to use loop for gridview control like this for(int x = 0;x<grdview1.Rows.Count; x++) { Linkbutton mylink = (Linkbutton)gridview.Row[x].FindControl("linkbutton1"); } hope this will help you No we can
how to find link button value from template field of gridview on pageload. . try this way LinkButton lnk = GridView1.FindControl( "linkbutto1" ); sorry for wrong post try this foreach ( GridViewRow row in GridView1.Rows LinkButton lnk = ( LinkButton )row.FindControl( "lnkSelectRow" ); Response.Write( " " + lnk.Text); } Hi This IS Code for Finding LinkButton value Which is There in TemplateField protected void Page_Load( object sender, EventArgs e) { foreach (GridViewRow
Hi Please send code to display dynamic message on mouseover of a link button in gridview Regards hi, you can use javascript with linkbutton. You need to use this code on rowbound event of girdview like this protected void grdCountry_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType = = DataControlRowType.DataRow) { ((LinkButton)e.Row.FindControl("btnShow")).Attributes["onclick"] = "javascript:alert("Your message here");"; } } hope this will help you On the RowDataBound event of GridView, write some code like this: LinkButton AddButton = (LinkButton)e.Row.FindControl("lnkAdd"); if (AddButton ! = null) AddButton.Attributes.Add("onmouseover", "window.status
findcontrol in gridview row command ASP.NET 06-Jun-13 10:07 AM Hi, How to find a control in gridview row command event. Its very urgent. Help need. hi, Just make sure that you are only binding your gridview at once in PageLoad with !Page.IsPostback method and try to specify the index of inside the Grid. Then try this DropDownList ddl = (DropDownList)GridView1.Rows[e.RowIndex].Cells[0].FindControl( "DropDownList1" ); string val = ddl.SelectedValue; Hope this will help you HI Are you trying to row was clicked. If the link button is in your first cell, you can do: LinkButton addButton = (LinkButton)e.Row.Cells[0].Controls[0]; Or you should be able to do e.Row.FindControl( "idofbutton" ) HI Be sure that you are setting a CommandName for your LinkButton like below
hi . . . i've nested gridview. . i want find the nested gridview control in RowCommand Event . . pls Give me solution HI here are good examples with code codeproject.com / KB / webforms / GridViewInsideGridView.aspx http: / / forums.asp.net / t / 1638799.aspx / 1?Nested+gridview+Find+the+parent+conrol+id http: / / msdn.microsoft.com / en-us / library / system.web.ui.webcontrols.gridview.rowcommand.aspx#Y1120 protected void GridView1_RowCommand( object sender, GridViewCommandEventArgs e) { if (e.CommandName = = "Select" ) { GridViewRow row = ( GridViewRow )((( LinkButton )e.CommandSource).NamingContainer); GridView grid2 = ( GridView )row.FindControl( "NestedGridview" ); } } For that you have to implement GridView1_RowCommand() event. use this
<table > <tr> <td> <asp:GridView ID = "gridview1" runat = "server"> <asp:TemplateField HeaderText = "View"> <ItemTemplate> <asp:LinkButton ID = "lnkview" Text = "View" runat = "server"> < / asp:LinkButton> < / ItemTemplate> < / asp:TemplateField> <asp:TemplateField HeaderText = "Doc's"> <ItemTemplate> <asp LinkButton ID = "lnkdocs" Text = "Doc's" runat = "server"> < / asp:LinkButton> < / ItemTemplate> < / asp:TemplateField> < / Columns> < / asp:GridView> < / td> < / tr> <tr> <td> < / td> < / tr> Add linkButton in itemTEmplate: < ItemTemplate > < asp : LinkButton ID = "lnkd
I have this gridview. . nw on click of the "editform" button, i want to redirect it to another page with the parameter of visitno. my code is as below. . :: <asp:GridView ID = "GridView1" runat = "server" AutoGenerateColumns = "False" CellPadding = "4" ForeColor = "#333333" GridLines = "None" Width = "1000px"> <AlternatingRowStyle HeaderText = "FromCity" / > <asp:BoundField DataField = "ToCity" HeaderText = "ToCity" / > <asp:TemplateField HeaderText = "Edit Form"> <ItemTemplate> <asp:LinkButton ID = "linkeditform" runat = "server" PostBackUrl = " "> EditForm< / asp:LinkButton> < / ItemTemplate> < / asp:TemplateField> <asp:TemplateField HeaderText = "Print Form"> <ItemTemplate> <asp:LinkButton ID = "linkprint" runat = "server"> PrintForm< / asp:LinkButton> < / ItemTemplate> < / asp:TemplateField> < / Columns> <EditRowStyle BackColor = "#999999" / > <FooterStyle BackColor = "#5D7B9D" Font-Bold = "True" ForeColor = "White
how to insert one gridview in another gridview. . .???? HI use this code protected void grdMaster_RowDataBound( object sender, System.Web.UI.WebControls.GridViewRowEventArgs e default (DataControlRowState); objListItem = e.Row.RowState; int intMAsterID1 = 0; if (e.Row.RowType = = DataControlRowType.DataRow) { GridView grd = default (GridView); if (objListItem = = 5) { grd = (GridView)e.Row.FindControl( "grdChildGridEdit" ); MasterTableID = Convert.ToInt32(((DataRowView)e.Row.DataItem).Row.ItemArray[0].ToString()); intMAsterID1 = MasterTableID; } else if (objListItem = = DataControlRowState.Normal | objListItem = = DataControlRowState.Alternate) { grd = (GridView)e.Row.FindControl( "grdChildGridNormal" ); intMAsterID1 = Convert.ToInt32(((DataRowView)e.Row.DataItem).Row.ItemArray[0].ToString