Domanda

I'm trying to pass information from js to codebehind. When setting a breakpoint at the end of the js - The (Firefox) debugger (-js) shows me that an input's value has been set, while the (Firefox) Inspector (-html) shows it hasn't. When reaching the codebehind - it hasn't been set. Why?

js to set the value and click a button that will then call the codebehind:

<script type="text/javascript">
    function doit(s, input, button)
    {
        var i = document.getElementById(input);
        i.innerHTML = s;
        var b = document.getElementById(button);
        b.click();
    }       
</script>

html with hidden input and button (for calling the codebehind) and a button to begin all of this, and a div to show the result:

<asp:Button ID="Button1" runat="server" Text="Test" OnClick="Button1_Click" />
<div style="display: none;">
    <input id="info" runat="server" />
    <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" />
</div>
<div id="result" runat="server">
</div>

C# codebehind to call the js, and to show the result:

protected void Button1_Click(object sender, EventArgs e)
{
    ClientScriptManager cs = Page.ClientScript;
    Type pageType = GetType();
    cs.RegisterStartupScript(
        GetType(),
        "aName",
        "doit('abc', '" + info.ClientID + "','" + Button2.ClientID + "');", 
        true);
}

protected void Button2_Click(object sender, EventArgs e)
{
    result.InnerText = info.Value;
}
È stato utile?

Soluzione

You need to set value property of input instead of innerHTML

Change

i.innerHTML = s;

To

i.value = s;

Altri suggerimenti

You want to use value to set the value of an input:

function doit(s, input, button) {
    document.getElementById(input).value = s;
    var b = document.getElementById(button);
    b.click();
}       
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top