Skip to content Skip to sidebar Skip to footer

Get Html Value On Web User Control Without Runat="server"

I'm new to this world. As you can see in the picture. This is my Default.aspx page where I have implemented a Web User Control(uc) called 'adress.ascx'. My uc control is divided i

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**
 protected void Page_Load(object sender, EventArgs e)
{
    if (Request.Form["__EVENTTARGET"] != null && 
    Request.Form["__EVENTTARGET"] == "btnAsp1")
    {
        btnAsp1_Click(null, null);
    }
}

private void btnAsp1_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>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>

             <uc1:Add runat="server" ID="ucAdres" WidthFirstCollumn="175px" />


    </body>
    </html>

And here is the code for WebUserControl.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>



<form  method="post" runat="server"
>
<h1>ASP.NET Controls</h1>
<br />
<asp:Label runat="server" Text="Street" ID="lable"></asp:Label>
<asp:TextBox runat="server" ID="tbStreet" Text="Text Asp.net text box"></asp:TextBox><br />
<br />
<asp:Button ID="btnAsp" runat="server" Text="Button Asp.net" OnClick="btnAsp_Click"/><br />
    <input type="submit" id="btnHtml" value="Html Button" />

<h2>-------------------------------</h2>

    <h1>Html Controls</h1>
    Street

    <input type="text" name="street" id="street" value="Text coming from HTML5"">
    <br />
    <br />

    <button type="button" id="btnStreet">Button HTML!</button>

    </form>

And here is the code of WebUserControl.ascx.cs

protected void btnAsp_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:

string value = 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:

protected void btnAsp_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""