Dynamically Created link Button click event Not Working

Posted by Shijohnjoseph under C# on 4/19/2011 | Points: 10 | Views : 6146 | Status : [Member] | Replies : 4
Hi ,
am creating linkbuttons dynamically using the following code ,
but its click event is not working please anyone help me ...
Its urgent for my ongoing project..
Here the Code:-
string strData = ds.Tables[0].Rows[0][3].ToString();

char[] separator = new char[] { ',' };

string[] strSplitArr = strData.Split(separator);

foreach (string arrStr in strSplitArr)
{
TableRow tr = new TableRow();
TableCell td = new TableCell();
LinkButton lnkBtn = new LinkButton();
lnkBtn.ID = "Lbtn_RelatedDest" + arrStr;
id = lnkBtn.ID;
lnkBtn.Text = arrStr;
lnkBtn.Click += new EventHandler(Lbtn_Click);
td.Controls.Add(lnkBtn);
tr.Cells.Add(td);
Table1.Rows.Add(tr);
}

"Personality has the power to open many doors, but character must keep them open"


Responses

Posted by: Ndebata on: 4/19/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Joseph,

May I know where you are adding these buttons,
If you adding these on page load make sure that you are not restricting the creation on postback.

Thanks,
Debata

Shijohnjoseph, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shijohnjoseph on: 4/19/2011 [Member] Starter | Points: 25

Up
0
Down
Hi , i tried it ..
i can post my code here please do check ..and give me a solution

protected void Page_Load(object sender, EventArgs e)
{
//if (!this.IsPostBack)
//{
string Destination;

if (Request.QueryString["Destination"]==null)
{
Destination = "Alappuzha";
}
else
{
Destination = Request.QueryString["Destination"].ToString();
}
DataSet ds = Obj.DestinationDtls(Destination);

Lbl_Destination.Text = ds.Tables[0].Rows[0][0].ToString();
Image1.ImageUrl = "~//DestinationImages/" + ds.Tables[0].Rows[0][1].ToString();
Lbl_Content.Text = ds.Tables[0].Rows[0][2].ToString();




string strData = ds.Tables[0].Rows[0][3].ToString();

char[] separator = new char[] { ',' };

string[] strSplitArr = strData.Split(separator);
int arrlength = strSplitArr.Length;


foreach (string arrStr in strSplitArr )
{
//Here getting the indexvalue of array using indexof method
int i = Array.IndexOf(strSplitArr, arrStr);
// creating table row
TableRow tr = new TableRow();
//creating table cell

TableCell td = new TableCell();
//adding style to td
td.CssClass = "RelatedDestination";
LinkButton lnkBtn = new LinkButton();
lnkBtn.ID = "Lbtn_RelatedDest" + i.ToString();

//id = lnkBtn.ID;
lnkBtn.Text = arrStr;
lnkBtn.CommandName = arrStr.ToString();

lnkBtn.Click += new EventHandler(myFunction);
td.Controls.Add(lnkBtn);
tr.Cells.Add(td);
Table1.Rows.Add(tr);
div1.Controls.Add(Table1);
//i++;
}

//}

}

"Personality has the power to open many doors, but character must keep them open"

Shijohnjoseph, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shijohnjoseph on: 4/19/2011 [Member] Starter | Points: 25

Up
0
Down
and my event is this
void myFunction(object sender, EventArgs e)
{
LinkButton lbtn = (LinkButton)sender;
string RelatedDest = lbtn.CommandName;

}

"Personality has the power to open many doors, but character must keep them open"

Shijohnjoseph, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 4/19/2011 [Member] Starter | Points: 25

Up
0
Down
Hi

Could please take the line
"div1.Controls.Add(Table1);
" out side of loop , every thing is fine do you want me to send u a sample file?

Thanks,
Debata

Shijohnjoseph, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response