0

I'm new to this world.

enter image description here

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 in 2 categories, first is Asp.net controls category and second is Html Controls category.

What is the scenario: I want to copy the value from Html Textbox to Asp:TextBox.

I have 2 options, either I can do with asp:Button or with html button(without runat="server")

Here is my code

<h1>ASP.NET Controls</h1>
<br />
<asp:Label runat="server" Text="Street"></asp:Label>
<asp:TextBox runat="server" ID="tbStreet" Text=""></asp:TextBox><br /><br />
<asp:Button ID="btnAsp" Text="Button ASP" runat="server" 
OnClick="btnAsp_Click" 
/>

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

<h1>Html Controls</h1>
Street
<input type="text" name="Street" id="street"><br />
<br />
<button type="button" id="btnStreet">Button HTML!</button>

And here is my code "code behind" in c#

protected void btnAsp_Click(object sender, EventArgs e)
{
    string value = Request.Form["street"];
}

When I run this code, the value I get is null enter image description here

13
  • did you try to put name="street" on you asp:TextBox? Commented Oct 30, 2017 at 9:52
  • I take it you do have a form control on the page? Commented Oct 30, 2017 at 9:54
  • @ElmerDantas yes I have tried both name and id both but no luck. Commented Oct 30, 2017 at 10:00
  • @Fred Yes I have a form on the control Commented Oct 30, 2017 at 10:00
  • add type="submit" to the asp:button and read the values in the method the action of the form is set to. Commented Oct 30, 2017 at 10:03

3 Answers 3

0
 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());
} 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much @Dhawal but I'm looking for something very simple. I'm really surprised that MS did not think about that.
Pleasure is all mine @user
0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.