c# - How to access a server side method from a dynamicaly generated html button by innerhtml of a div -
i want access c# method html button dynamicaly generated innerhtml of div.
code
using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; public partial class demo2 : system.web.ui.page { private string dynamictextboxvalue; protected void page_load(object sender, eventargs e) { if (!ispostback) { textpreview.innerhtml ="<input type=\"submit\" name=\"btnsubmit\" id=\"btnsubmit\" value=\"click here\" runat=\"server\" onclick=\"get_data\"/>"; } } public void getdata() { response.write("clicked."); }
}
i want access getdata() method html button..help.plz
we can not call server side function directly client (without registering on server, refer @fubo answer below this), if can security threat. call function browser adding raw html web page, case.
however can postback , in page_load can validate our input button call desired method.
protected void page_load(object sender, eventargs e) { if (!ispostback) { testdiv.innerhtml = @"<input type=""button"" id=""btnsubmit"" value = ""click here"" runat = ""server"" onclick=""__dopostback('btnsubmit','')""/> "; } else { var eventtarget = request.form["__eventtarget"].tostring(); if (eventtarget == "btnsubmit") { getdata(); } } } public string getdata() { response.write("clicked."); }
Comments
Post a Comment