开发者

Firing an asp:button through Javascript

I have an asp:textbox and an asp:button that submits that textbox. I then have javascript code that creates a div that contains a textarea and an input[type=button].

I want to be able to submit these created textarea's text using the asp:button event. S开发者_JS百科o when a user enters text within one of the div created textarea's and clicks the div input:button, I want to copy the textarea text to the asp:textbox and then call the asp:button click event through javascript. I'd like to somehow pass the id of the div input:button into the click event of the asp:textbox.

How do I pass a variable to the OnClick event of an asp:button through javascript?

I've thought of using a hidden asp:label to store the id, but it seems like there should be a better way.


You could try this:

<asp:ScriptManager ID="scm" runat="server" />
<div>
   <div id="clickme" style="width: 100px; height: 50px; background-color: #CCC"
        onclick="__doPostBack('ctl00$MainContent$Button1', '');" />
</div>
<input type="text" name="textbox" />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />


protected void Button1_Click(object sender, EventArgs e)
{
    string text = Page.Request.Params["textbox"];
}

Note the hardcoded name of the button-id.

__doPostBack will fire a postback and the Button1_Click method gets called.

You don't need to copy the text to a asp:TextBox because you can get the content of the input field from Page.Request.Params.

Edit: Don't forget to embed a ScriptManager when using javascript in your page.


Why do you have to copy the data into server side controls? You can always get the current HttpRequest (HttpContext.Current.Request) on the server side and you will have all the form data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜