0

The page has table inside updatePanel and linkbutton inside tablecell:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
  <ContentTemplate>
    <asp:Table ID="Table1" runat="server" BackColor="White" BorderColor="Black" 
        BorderWidth="1px" ForeColor="Black" GridLines="Both" BorderStyle="Solid">
    </asp:Table>
  </ContentTemplate>        
</asp:UpdatePanel>  

Inside .cs file I add rows to the table:

TableRow tr = new TableRow();
TableCell tc = new TableCell();
LinkButton lb = new LinkButton();
lb.Text = "Click me";
lb.Click += new EventHandler(this.LinkButton_Click);
tc.Controls.Add(lb);
tr.Cells.Add(tc);
Table1.Rows.Add(tr);

And the method:

protected void LinkButton_Click(Object sender, EventArgs e)
{
  Response.Write("<script type='text/javascript'>");
  Response.Write("alert('Ok!');");
  Response.Write("</script>");  
}

So, when I click linkbutton the alert doesn't work. What is a problem?

Note that, if I delete updatepanel the linkbutton works when clicking at it.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Nurlan
  • 2,860
  • 19
  • 47
  • 64

2 Answers2

2

It is not the correct way

Use this

ScriptManager.RegisterStartupScript(pnlUpdateCom, this.GetType(), "UpdateCom", "alert('OK!.');return false;", true);

Pushpendra
  • 814
  • 1
  • 6
  • 17
  • The function not only uset for alerting messages, but also has another functions.. – Nurlan Sep 24 '12 at 11:43
  • 1
    You can write any javascript code inside it. ScriptManager.RegisterStartupScript(pnlUpdateCom, this.GetType(), "UpdateCom", "javascript code", true); You can any javascript function – Pushpendra Sep 24 '12 at 13:28
  • One question. Is it possible to work with server control, for example with asp.net table inside javascript function? For instance, I want to add new rows to asp.net table using variables inside .cs file? – Nurlan Sep 25 '12 at 08:23
  • Your question is not clear! You want to add rows to asp.net table using javascript i.e. from client side or in .cs file. Using server side its easy and I expect you have the idea. Using javascript first try implementing using javascript function from aspx page and when you are getting the required output just call that function using RegisterStartupScript. – Pushpendra Sep 25 '12 at 09:31
  • I fill asp.net table in C#, i.e. TableRow tr = new TableRow(); TableCell tc = new TableCell(); tc.Text = myarray[i]; tr.controls.add(tc); table1.rows.add(tr); So, is it possible to implement this code using javascript function? – Nurlan Sep 25 '12 at 11:48
  • Frankly speaking I have not tried this yet as I have not got a chance for such implementation. I believe it can be done in Javascript but you might face issues while reading the same from Table on server side. – Pushpendra Sep 25 '12 at 12:00
0

You need to add postbacktrigger as following:

<asp:PostBackTrigger ControlID="SearchBrn"/>
Altaf Patel
  • 1,351
  • 1
  • 17
  • 28