Reputation: 223
I am generating dynamic link buttons in c#.At click of any of them,a other function will be call that will show which link button was clicked.But it is not being call at click of any link button.
This is how i am generating it.
Int32 i; //create a integer variable
for (i = 1; i <= 10; i++) // will generate 10 LinkButton
{
LinkButton lb = new LinkButton(); //create instance of LinkButton
lb.Text = Convert.ToString(i) + ""; //LinkButton Text
lb.ID = Convert.ToString(i); // LinkButton ID’s
lb.CommandArgument = Convert.ToString(i); // LinkButton CommandArgument
lb.CommandName = Convert.ToString(i); // LinkButton CommanName
lb.OnClientClick+= new CommandEventHandler(lb_Command); //Create Handler for it.
//type lb.Command += and press double time Tab Key it will generat the lb_Command() code
PlaceHolder1.Controls.Add(lb); // Adding the LinkButton in PlaceHolder
}
This is function code.
void lb_Command(object sender, CommandEventArgs e)
{
Label1.Text = e.CommandName; // will display the which Linkbutton clicked
Label1.Text = "aaaa";
// Response.Redirect(“LnkBtn.aspx?val=” + Label1.Text); // you can also use as QueryString to send values to another page
}
please provide help.
Thanks.
Upvotes: 2
Views: 2934
Reputation: 1774
Instead of the below code
lb.OnClientClick+= new CommandEventHandler(lb_Command); //Create Handler for it.
use the following
lb.Command+= new CommandEventHandler(lb_Command); //Create Handler for it.
Here is the full code. It running at my end.
protected void Page_Load(object sender, EventArgs e)
{
Int32 i; //create a integer variable
for (i = 1; i <= 10; i++) // will generate 10 LinkButton
{
LinkButton lb = new LinkButton(); //create instance of LinkButton
lb.Text = Convert.ToString(i) + " "; //LinkButton Text
lb.ID = Convert.ToString(i); // LinkButton ID’s
lb.CommandArgument = Convert.ToString(i); // LinkButton CommandArgument
lb.CommandName = Convert.ToString(i); // LinkButton CommanName
//lb.Click += lb_Click; //Create Handler for it.
lb.Command += lb_Command;
//type lb.Command += and press double time Tab Key it will generat the lb_Command() code
form1.Controls.Add(lb); // Adding the LinkButton in PlaceHolder
}
}
void lb_Command(object sender, CommandEventArgs e)
{
Label1.Text = e.CommandName; // will display the which Linkbutton clicked
}
Thanks.
Upvotes: 0
Reputation: 4907
You are using the wrong event :
ClientClick is just a client side event triggered in javascript.
What you want is the Click Event
[...]
lb.Click += new CommandEventHandler(lb_Command); //Create Handler for it.
If you still aren't catching the event, then chances are you are declaring your LinkButton dynamically at the wrong time in the Page Lifecycle (as mentionned by Eoin Campbell in his comments and in his answer).
He links to an article that's really good on the subject, you should read it to understand why you aren't catching the event.
Judging by your comments on your question and on different answers, you are declaring the button in your Page_Load function. That will not work because Page_Load occurs too late in the page lifecycle.
From what I understand, right now you have :
void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
Int32 i; //create a integer variable
for (i = 1; i <= 10; i++) // will generate 10 LinkButton
{
LinkButton lb = new LinkButton(); //create instance of LinkButton
lb.Text = Convert.ToString(i) + ""; //LinkButton Text
lb.ID = Convert.ToString(i); // LinkButton ID’s
lb.CommandArgument = Convert.ToString(i); // LinkButton CommandArgument
lb.CommandName = Convert.ToString(i); // LinkButton CommanName
lb.OnClientClick+= new CommandEventHandler(lb_Command); //Create Handler for it.
PlaceHolder1.Controls.Add(lb); // Adding the LinkButton in PlaceHolder
}
}
}
You need to get rid of that. Instead, use :
void Page_Init(object sender, EventArgs e)
{
Int32 i; //create a integer variable
for (i = 1; i <= 10; i++) // will generate 10 LinkButton
{
LinkButton lb = new LinkButton(); //create instance of LinkButton
lb.Text = Convert.ToString(i) + ""; //LinkButton Text
lb.ID = Convert.ToString(i); // LinkButton ID’s
lb.CommandArgument = Convert.ToString(i); // LinkButton CommandArgument
lb.CommandName = Convert.ToString(i); // LinkButton CommanName
lb.OnClientClick+= new CommandEventHandler(lb_Command); //Create Handler for it.
PlaceHolder1.Controls.Add(lb); // Adding the LinkButton in PlaceHolder
}
}
The article linked above will explain why that is. It's a hard concept to grasp but a very important one.
Upvotes: 3
Reputation: 44268
Ok. two things. as others have pointed out you should be using the LinkButton
Command
or Click
handlers.
But your second problem is that you are running into Page Life Cycle issues by attempting to only generate your controls
Page_Load
.Read this: It's a very good article on the subject that dates all the way back to .net 1 https://web.archive.org/web/20210330142645/http://www.4guysfromrolla.com/articles/092904-1.aspx
In a nutshell, you should be creating your controls every single time (not just on the initial post) and you need to wire up your event handler (the +=
part) early enough in the page lifecycle such that the events are wired, by the time the PageLifeCycle attempts to trigger them.
Override the page's OnInit
method and move your code there, without the if(!Postback)
check
Upvotes: 1
Reputation: 2480
If link button is inheriting from Button class then you should attach to the OnClick event. ?
http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.buttonbase.click(v=vs.100)
or
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onclick.aspx
Upvotes: -1
Reputation: 12675
You are mixing client and server side code. OnClientClick is method for attaching client side code. lb_Command runs on the server side.
Use Click instead.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclientclick.aspx versus http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.click.aspx
Upvotes: 2