Click here to Skip to main content
15,921,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I submit a web user control with javascript (not attached to a button)?

If I do it like form1.submit() and the page has multiple controls I think it will make everything postback.

Does anyone know the syntax for something like WebUserControl.Submit() ?

Thanks
Posted

Before going a step ahead, let me warn you first only that when it will be a postback, it will be the Page_Load of .aspx page having that user control which will fire first and then the Page_Load of that particular usercontrol (.ascx) of your interest.

If you are okay with this, then just let me know at what event do you want the submit action of user control through javascript?
 
Share this answer
 
Comments
fjdiewornncalwe 8-Mar-11 16:08pm    
OP Comment moved from answer:
Thanks for the reply Rajiv.

Yes thats sounds fine.

Basically the reason I need this functionality is someone signing up to one of my lists.

Here is flow:

1. user signs up and clicks button
2. in my code behind I check to see if they already signed up, and if they have I output a Javascript Confirm() message asking if they want the confirmation email re-sent.
3. if they answer yes then then the javascript will alter a hidden field on the form to let my codebehind know what is going on and then submit the form automatically (this is the bit I need this code for).

Alternatively if there is an easier way to capture the response from a yes/no box then that will be good also Smile | :)
The fact that user control is again a control. Posting/ submitting is for a form. User control is within a form. So it is not posting.

If you want to update data in the user control then think of update panel, the ajax way in asp.net

In your case, after the user click once user clicks the sign up button, verify is he already sign up. If already sign up then in the page load event execute a javascript using the ClientScript.RegisterStatupScript function. From the javascript you can set the hidden field.

For RegisterStartupScript function refer

http://msdn.microsoft.com/en-us/library/system.web.ui.page.registerstartupscript.aspx[^]
 
Share this answer
 
Comments
fjdiewornncalwe 8-Mar-11 16:08pm    
OP Comment moved from answer:
Yes, you have described what I am doing but this doesn't solve the problem.

I send a confirm() javascript dialogue to user, I then need page to postback if he says yes so I need to know the javascript to make the web user control postback.
I found the solution in a guide, sample code was in VB so here is my c# translation:

Put this in pageload:

C#
PostBackStr = Page.ClientScript.GetPostBackEventReference(this, "ConfirmedResend");

string eventArg = Request["__EVENTARGUMENT"];
if (Page.IsPostBack)
{
    if (eventArg == "ConfirmedResend")
    {
        Response.Write("It works"); // code goes here that I want to run triggered from JavaScript
    }
}


Then on the page put this:

XML
<script type="text/javascript" language="javascript">
function doPostB()
    {
    <%= PostBackStr %>;
    }
</script>



Then just trigger doPostB() and it will postback and run code inside the if() statement.
 
Share this answer
 
It sounds like you want to postback only a portion of the page using JavaScript. According to this page, you can do that like so:
ASP.NET
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel runat="server" ID="UpdatePanel1" OnLoad="UpdatePanel1_Load">
  <ContentTemplate>
    <asp:Label runat="server" ID="Label1" />
  </ContentTemplate>
</asp:UpdatePanel>

The UpdatePanel is required to postback only a portion of the page. The ScriptManager is necessary to use any AJAX stuff (in this case, the UpdatePanel). The Label is for demonstration purposes.
HTML
<div id="Container" onclick="__doPostBack('UpdatePanel1', '');">

That shows how to make a postback for just one UpdatePanel from JavaScript. In this case, it is initiated from a click event.
VB.NET
protected void UpdatePanel1_Load(object sender, EventArgs e)
{
  Label1.Text = DateTime.Now.ToString();
}

That shows how you can use server-side code to update the client during your UpdatePanel postback.
 
Share this answer
 
Comments
Guerrilla123 8-Mar-11 18:10pm    
Yes, I saw this solution in another guide but wanted to do it without using Ajax.

The code in my previous post seems to work perfectly. Is the update panel approach better?
AspDotNetDev 8-Mar-11 18:50pm    
Your approach looked as if it would post back the entire page, but I could be wrong about that. I thought posting back the entire page is what you were trying to avoid.
Guerrilla123 8-Mar-11 19:30pm    
Ahh I see what your saying. When I postback this way with the custom arguement only the code inside the if statement seems to run.

I think I may run into problems if the control is on the same page as a control that uses a .net validation control but I am not sure as i havent tested it.

Perhaps the updatepanel method would be the better way. I avoided it because other developer has used update panel and AJAX on site and anytime I use it I get problems (I am still fairly new to programming)

I will give it a go on a page with a validation control tomorrow and report back
Guerrilla123 9-Mar-11 12:53pm    
I tested it on page with another control that used a required field validation control and the postback didn't cause it to validate so I think it will work ok.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900