Sunday, 24 February 2013

How to fire onClick event of LinkButton inside gridview in asp.net using c#


.aspx page
 

<asp:gridview ID="GridView1" runat="server" Width="100%" EnableTheming="false" AutoGenerateColumns="false">

<Columns>

<asp:TemplateField HeaderText="Customer ID">

<ItemTemplate>

<asp:LinkButton ID="lnk_CustomerID" Text='<%# Bind("Customer_ID")%>' runat="server" OnClick="lnk_CustomerID_Click"></asp:LinkButton>

</ItemTemplate>


</asp:TemplateField>

<asp:TemplateField HeaderText="Customer Name">

<ItemTemplate>

<asp:Label ID="lbl_CustomerName" Text='<%# Bind("Customer_Name")%>' runat="server"></asp:Label>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Customer Age">

<ItemTemplate>

<asp:Label ID="lbl_CustomerAge" Text='<%# Bind("Customer_Age")%>' runat="server"></asp:Label>

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:gridview>


As you can see that I have Link button inside the gridview that in mycase contains the unique values which is Customer ID. Now I want to fire the onclick event of LinkButton to execute some other code, which is to store the Text of LinkButton in a session and then want redirection to the Customer Detail page. Let’s have a look how it can be done.

.cs page

protected void lnk_CustomerID_Click(object sender, EventArgs e)
{
LinkButton link = (LinkButton)sender;
GridViewRow gv = (GridViewRow)(link.Parent.Parent);
LinkButton CustomerID = (LinkButton)gv.FindControl("lnk_CustomerID");
Session["customerid"] = CustomerID.Text;
Response.Redirect("customer_detail.aspx");
}
So this is the way to fire the onclick event of LinkButton inside gridview in asp.net using c#.

Maintaining state for logged Users

  1.    Application.Lock();  
  2.           
  3.         int clickCounter = 0;  
  4.         if(Application["ClickCounter"] !=null)  
  5.         {  
  6.             clickCounter = (int)Application["ClickCounter"];  
  7.         }  
  8.         clickCounter++;  
  9.         Application["ClickCounter"] = clickCounter;  
  10.         Application.UnLock();  
  11.   
  12.         Label1.Text = "Button Clicked: " + clickCounter.ToString() + " times";

Extracting Nupkg files using command line

Rename it to zip first then extract files as below 1.     Rename-Item -Path A_Package.nupkg -NewName A_Package.zip 2.     Make sure to repla...