By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,333 Members | 1,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,333 IT Pros & Developers. It's quick & easy.

How to call a specified url, outside of event click, to redirect

P: 1
How to call a specified url, outside of event click, to redirect?

Expand|Select|Wrap|Line Numbers
  1. public void Page_Load(object sender, EventArgs e)
  2.     {
  3.         qry = "Select slink from Sites";
  4.         OleDbConnection con = new OleDbConnection(global.cs1);
  5.         OleDbCommand com = new OleDbCommand(qry, con);
  6.         con.Open();
  7.         OleDbDataReader re = com.ExecuteReader();
  8.  
  9.         while (re.Read())
  10.         {
  11.             i++;
  12.             Table tb = new Table();
  13.             tb.ID = "Table" + i;
  14.             this.form1.Controls.Add(tb);
  15.             TableRow Tr = new TableRow();
  16.             Tr.ID = "TableRow" + i;
  17.             tb.Controls.Add(Tr);
  18.             TableCell tc = new TableCell();
  19.             tc.ID = "TableCell" + i;
  20.             Tr.Controls.Add(tc);
  21.             LinkButton lnk = new LinkButton();
  22.             lnk.ID = "lnk" + i;
  23.             tc.Controls.Add(lnk);
  24.             lnk.Text = "" + re["slink"];
  25.             j = lnk.Text;
  26.             lnk.Click += new System.EventHandler(lnk_Click);
  27.             lbladd.Controls.Add(tb);
  28.         }
  29.         con.Close();
  30.     }
  31.  
  32.     public void lnk_Click(object sender, EventArgs e)
  33.     {
  34.      //Here is problem to redirect to that particular link 
  35.     }
Aug 2 '12 #1
Share this Question
Share on Google+
1 Reply


Frinavale
Expert Mod 5K+
P: 9,731
You are dynamically creating LinkButtons within your PageLoad event.

The problem with this is that when the LinkButton is clicked, it posts back to the server for processing the Click event.

But the Click Event for the LinkButton is generated in the PageInit event.

The PageInit event occurs before the PageLoad event and so since the LinkButton doesn't exist at that time, the Click Event is never generated...

This is why your lnk_Click method is never being executed.

So, instead of dynamically creating the LInkButtons in the PageLoad event, I recommend that you use a Repeater Control to dynamically create your table (with links within it).

-Frinny
Aug 2 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.