Dynamic Link Button not doing anything on click
I'm trying to fix a problem with some code (not written by me)
lnkbtnPageNumber.ID = "PageNumberCustomerRef" + intPageNumber.ToString();
lnkbtnPageNumber.Command += new CommandEventHandler(lnkbtnPageNumber_Command);
lnkbtnPageNumber.CommandName = "DepotRef";
lnkbtnPageNumber.CommandArgument = intPageNumber.ToString();
lnkbtnPageNumber.Text = intPageNumber.ToString().PadLeft(3, '0');
lnkbtnPageNumber.Attributes.Add("style", "margin: 2px;");
pDepotRefPages.Controls.Add(lnkbtnPageNumber);
This code creates a link button, however when I click on the button on the page. The function lnkbtnPageNumber_Command is not being called. The scripts just not getting to it.
I've tried google but everywhere I've looked says that this code sho开发者_运维问答uld work fine.
Here is the code which is being called by the function:
void lnkbtnPageNumber_Command(object sender, CommandEventArgs e)
{
try
{
switch (e.CommandName)
{
case "GlobalID":
gintDocketNumberPage = Convert.ToInt32(e.CommandArgument);
break;
case "CreatedDate":
gintCreationDatePage = Convert.ToInt32(e.CommandArgument);
break;
case "Accounts":
gintAccountPage = Convert.ToInt32(e.CommandArgument);
break;
case "CustomerRef":
gintCustomerRef = Convert.ToInt32(e.CommandArgument);
break;
case "DepotRef":
gintDepotRef = Convert.ToInt32(e.CommandArgument);
break;
default:
gintDocketNumberPage = Convert.ToInt32(e.CommandArgument);
break;
}
I'm really stumped! please let me know if I've left out anything.
Thanks for any help you can provide!
I've just read somewhere that this code won't work unless called from Page_Init which if true is a but of a pain cos I can't put this function in Page_Init...
Where is this lnkbtnPageNumber, in the gridView Pager? If so, is it being regenerated on every postback or added f.e. in RowDataBound? The best place in that case would be in RowCreated because it will be called on every postback and all dynamic controls(and their events) must be recreated on postback.
There are three possibilities:
- You have to create the dynamic controls on Init event and add them to the parent control.
- You will have recreate the control in each page load.
- If you have done this, and still getting the error, inspect the ID of each and every control that you added dynamically and their parent controls as well. Ensure no special letters exist. Ensure ID values are unique. You will be fine.
精彩评论