Get Html Value On Web User Control Without Runat="server"
Solution 1:
I hope you will find this helpful .
**Aspx Page**
<input type='button' id='btnAsp1'value='Shop Now'class='form-control'value='Button:1' onclick='javascript:__doPostBack("btnAsp1");'
style='background-color:#3465aa; color:white' >
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET"value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT"value="" />
<script type="text/javascript">
//<![CDATA[var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
//]]>
</script>
**Code Behind**
protectedvoidPage_Load(object sender, EventArgs e)
{
if (Request.Form["__EVENTTARGET"] != null &&
Request.Form["__EVENTTARGET"] == "btnAsp1")
{
btnAsp1_Click(null, null);
}
}
privatevoidbtnAsp1_Click(object sender, System.EventArgs e)
{
Response.Write("You Clicked on " +
Request.Form["__EVENTARGUMENT"].ToString());
}
Solution 2:
Here is the solution. Code for Defualt.aspx page.
<%@ Register Src="~/WebUserControl.ascx" TagName="Add" TagPrefix="uc1"
%>
<!DOCTYPE html><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title></title></head><body><uc1:Addrunat="server"ID="ucAdres"WidthFirstCollumn="175px" /></body></html>
And here is the code for WebUserControl.ascx
<%@ ControlLanguage="C#"AutoEventWireup="true"CodeFile="WebUserControl.ascx.cs"Inherits="WebUserControl" %>
<formmethod="post"runat="server"
><h1>ASP.NET Controls</h1><br /><asp:Labelrunat="server"Text="Street"ID="lable"></asp:Label><asp:TextBoxrunat="server"ID="tbStreet"Text="Text Asp.net text box"></asp:TextBox><br /><br /><asp:ButtonID="btnAsp"runat="server"Text="Button Asp.net"OnClick="btnAsp_Click"/><br /><inputtype="submit"id="btnHtml"value="Html Button" /><h2>-------------------------------</h2><h1>Html Controls</h1>
Street
<inputtype="text"name="street"id="street"value="Text coming from HTML5""><br /><br /><buttontype="button"id="btnStreet">Button HTML!</button></form>
And here is the code of WebUserControl.ascx.cs
protectedvoidbtnAsp_Click(object sender, EventArgs e)
{
string value = Page.Request.Form["street"].ToString();
if (value != null)
{
tbStreet.Text = value;
} else
{
tbStreet.Text = "Nooooooooo";
}
}
I explain again, that you have a WebUserControl where you have 2 types of Controls(Asp.net and html). What you want to do is to copy from HTML to asp.net without runat="server". I often has problem of Form so finally I found that I have 2 Forms, 1 on WebUserControl and 1 on Default.aspx.
Solution 3:
Your screenshot shows a breakpoint on the following line:
stringvalue = Request.Form["street"];
with value being null
. However, from Using Breakpoints on MSDN (emphasis mine):
When you run this code in the debugger, execution stops whenever the breakpoint is hit, before the code on that line is executed
If you go to the next line, the line will run and populate value
. To copy this into the ASP.net control, the following should be sufficient:
protectedvoidbtnAsp_Click(object sender, EventArgs e)
{
tbStreet.Text = Request.Form["street"];
}
I would caution that what you are trying to do probably has a better solution, so even if this solves your immediate problem you may find that other issues crop up further down the line.
Post a Comment for "Get Html Value On Web User Control Without Runat="server""