c# - How to add runat=server attribute to html buttons -
i have multiple html buttons generated server side method on csharp class.
result is:
<button id='btn1' runat='server' onserverclick='btn_click'>apply</button> <button id='btn2' runat='server' onserverclick='btn_click'>apply</button> <button id='btn3' runat='server' onserverclick='btn_click'>apply</button> ... <button id='btnn' runat='server' onserverclick='btn_click'>apply</button>
i add btn_click event behind code:
protected void btn_click (object sender, eventargs e) { string id = (sender control).clientid; msg = id; }
as can see want buttonid when click on of buttons, btn_click won't call , can't buttonid.
i using asp.net website c#4.0.
the problem need generate buttons using server side , runat attribute doesn't work method . how can fix problem?
method body generated buttons
for (int = 0; < dt.rows.count; i++) { datetime dttemp = datetime.parse(dt.rows[i]["messagedate"].tostring()); if (dt.rows[i]["isread"].tostring() == "true") readed = "messagereaded"; else readed = "messagenew"; post += "<div class='modal fade' id='mymodal" + dt.rows[i]["messageid"].tostring() + "' tabindex='-1' role='dialog' aria-labelledby='mymodallabel'>" + "<div class='modal-dialog' role='document'>" + "<div class='modal-content'>" + "<div class='modal-header'><button type='button' class='close' data-dismiss='modal' aria-label='close'><span aria-hidden='true'>×</span></button><h4 class='modal-title' id='mymodallabel'><span style='font-weight:bold'>subject</span> : " + dt.rows[i]["subject"].tostring() + "</h4></div>" + "<div class='modal-header'><p><span style='font-weight:bold'>date</span> : " + dttemp.tostring("yyyy/mm/dd") + "</p>" + "<p><span style='font-weight:bold'>time</span> : " + dt.rows[i]["messagetime"].tostring() + "</p>" + "<p><span style='font-weight:bold'>email</span> : " + dt.rows[i]["email"].tostring() + "</p></div>" + "<div class='modal-body'>" + dt.rows[i]["message"].tostring() + "</div>" + "<div class='modal-footer'><button type='button' class='btn btn-default' data-dismiss='modal'>close</button>" + "<button id='btn" + dt.rows[i]["messageid"].tostring() + "' class='btn btn-danger' onclick='deletemessage_click'>delete</button></div>" + "</div></div></div>"; string narrow = special.timetonarrow(dt.rows[i]["messagedate"].tostring(), dt.rows[i]["messagetime"].tostring()); post += "<a data-toggle='modal' data-target='#mymodal" + dt.rows[i]["messageid"].tostring() + "' href='#' class='list-group-item " + readed + "'><span class='badge'>" + narrow + "</span><i class='fa fa-fw fa-comment'></i> <span>" + dt.rows[i]["name"].tostring() + "</span> : <span>" + dt.rows[i]["subject"].tostring() + "</span></a>"; }
so problem on
<button id='btn" + dt.rows[i]["messageid"].tostring() + "' runat='server' class='btn btn-danger' onserverclick='deletemessage_click'>delete</button>
edit post : can see can't use asp:button because using bootstrap modal. if think can still use asp:button please write code , show me how .thanks
as other's have said, problem asp.net won't recognise runat="server"
when generate controls string of html.
your code should done in different way, if wanted change have done work little differently.
instead of runat="server"
on buttons, make them submit buttons , assign them name , value, example...
instead of this:
<button id='btn1' runat='server' onserverclick='btn_click'>apply</button> <button id='btn2' runat='server' onserverclick='btn_click'>apply</button> <button id='btn3' runat='server' onserverclick='btn_click'>apply</button>
do (note have same name):
<button name="btn" type="submit" value="1">apply</button> <button name="btn" type="submit" value="2">apply</button> <button name="btn" type="submit" value="3">apply</button>
then in page_load event can detect if 1 of these buttons clicked following:
if (page.ispostback) { if (request.form["btn"] != null) { //a btn clicked, it's value int btn = int.parse(request.form["btn"]); //do btn number } }
here's little sample has 3 buttons , displays number of button clicked:
test.aspx
<%@ page language="c#" autoeventwireup="true" codebehind="test.aspx.cs" inherits="test" %> <!doctype html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <button name="btn" type="submit" value="1">apply</button> <button name="btn" type="submit" value="2">apply</button> <button name="btn" type="submit" value="3">apply</button> </form> </body> </html>
test.aspx.cs
using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; public partial class test : system.web.ui.page { protected void page_load(object sender, eventargs e) { if (page.ispostback) { if (request.form["btn"] != null) { int btn = int.parse(request.form["btn"]); response.write(btn); } } } }
Comments
Post a Comment