简体   繁体   中英

Right syntax of Button Onclick Event in Repeater

I use a repeater Control to display all the comments from Datatable :

COMMENT(id,content,time);

In repeater I insert a button Delete to Delete that correlative comment

I wonder if I can add an variable " string id " in ButtonDelete_Click() like:

protected void ButtonDelete_Click(object sender, EventArgs e, string id)
{
     int idcm = Convert.ToInt32(id);
     string sql = "delete from COMMENT where ID=" + idcm;
     l.EXECUTEQUERYSQL(sql);
     ErrorTrap("DONE");//alert deleted sucessfully
}

And in aspx page:

<Repeater...>
    <asp:Button ID="ButtonDelete" runat="server" Text="Delete comment" 
         OnClick="ButtonDelete_Click(<%#Eval("MA_COMMENT") %>)"/>
    ....
</Repeater>

But when I build this page, an error ocur: The server tag is not well formed. at line:

<asp:Button ID="ButtonDelete" runat="server" Text="Delete comment" 
     OnClick="ButtonDelete_Click(<%#Eval("MA_COMMENT") %>)"/>

It is the first time I use Repeater Control, so I really dont know how is the right syntax? And I wonder if I can add more variales in ButtonDelete_Click Event or not???

Help!

you can use the command argument property fot the button and grap it in the click event :

<asp:Button ID="ButtonDelete" runat="server" Text="Delete comment" 
         OnClick="ButtonDelete_Click()" CommandArgument='<%#Eval("MA_COMMENT") %>'/>

on the click event

void ButtonDelete (object sender, EventArgs e)
  {

     var id = int.parse(e.CommandArgument.ToString());

  }

Hope this is helpful

use this

OnClick='ButtonDelete_Click(<%#Eval("MA_COMMENT") %>)'

or try replacing ' to " it should work.

Edit 1

More details

  1. The server tag is not well formed
  2. The server tag is not well formed.(databinder.eval)
  3. error The server tag is not well formed
  4. The server tag is not well formed error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM