0

I write script like this in my .cs file :

 StringBuilder script = new StringBuilder();
 script.Append("<script type=\"text/javascript\"> function submitform(){");
 script.Append(" document.forms['" + ((HtmlGenericControl)frm).Attributes["id"] + "'].submit();} </");
 script.Append("script>");

How can i call this function in the OnClientClick of my link button ?

LinkButton hl_process = new LinkButton();
hl_process.OnClientClick = ""

Edit1:

protected Control CreateCommForm()
{
    HtmlGenericControl frm = new HtmlGenericControl("form");
    frm.Attributes.Add("id", "sal");
    frm.Attributes.Add("method", "post");
    frm.Attributes.Add("action", "https://------");
    /////////////////////////////////////////
    HtmlGenericControl hdn_sal_a = new HtmlGenericControl("input");
    hdn_sal_a.Attributes.Add("id", "hdn_sal_a");
    hdn_sal_a.Attributes.Add("name", "hdn_sal_a");
    hdn_sal_a.Attributes.Add("type", "hidden");
    hdn_sal_a.Attributes.Add("value", Session["emp_num"].ToString());
    /////////////////////////////////////////
    HtmlGenericControl hdn_sal_b = new HtmlGenericControl("input");
    hdn_sal_b.Attributes.Add("id", "hdn_sal_b");
    hdn_sal_b.Attributes.Add("name", "hdn_sal_b");
    hdn_sal_b.Attributes.Add("type", "hidden");
    hdn_sal_b.Attributes.Add("value", Session["user_name"].ToString());

    frm.Controls.Add(hdn_sal_a);
    frm.Controls.Add(hdn_sal_b);

    column1.Controls.Add(frm);
    return frm;

}
9
  • 1
    Looks like a classic case of bad separate of concerns... Commented Feb 29, 2012 at 9:29
  • Why the server handles the client side scripts? Commented Feb 29, 2012 at 9:30
  • i create forms according to some params in run time .then i need to submit each form in the onclient click of the link button created in the run time Commented Feb 29, 2012 at 9:36
  • O.k. after the edit... move all the HTML part to the ASPX file, it's place isn't in the .cs files Commented Feb 29, 2012 at 9:36
  • 1
    The Visual part your application shouldn't be affected if you move your app to java or ruby. that's what separate of concerns is. Commented Feb 29, 2012 at 9:38

2 Answers 2

2

separate the concerns The Visual part your application shouldn't be affected if you move your app to java or ruby. that's what separate of concerns is.

write the client script in the client, not in the cs file:

$('#<%= hl_process.ClientID %>').click(function(){
     ...
     $('#formId').submit();
     // if the button inside the form:
     this.form.submit(); // HTML5
     // Or:
     $(this).closest('form').submit(); 
     // if the button not inside the form :
     var class = $(this).attr('class');
     $('form.' + class).submit();
});
Sign up to request clarification or add additional context in comments.

6 Comments

there are more than form each form created in run time i wanna to submit with specific link button created in run time
no the button is n't in the form it is a link button out the form
@just_name. So give the same class to the button and to form. and select it with it, see my update
@just_name. I think you should open a new question about how separate the concerns with DB involved. This question is targeting too wide issue.
@just_name. It can done with ajax. But please open a new thread with all the relevant code and explanations. Good Luck.
|
1

Use jquery to bind to the click event instead of doing this on the server side:

<a href="#" class="blah">Submit Me</a>

then in javascript something like:

<script type="text/javascript">
$('.blah').click(function() {
   document.forms[0].submit();
});
</script>

Edit:

While you can generate UI elements with codebehind it's not quite the asp.net way. Use repeaters if you must repeat the generation of controls. Actually, creating multiple forms is not the asp.net way either, as it assumes only one form running at the server context and everything else binds to an event on submission. Anyways, it seems you're still learning asp.net and probably coming form PHP or something similar.

To accommodate your request, I'd advice to stay away from from generating JS on the server side. Give different class names to your forms and use the same method above. You don't need a LinkButton to submit the form, a simple anchor <a> fits the bill.

You can use the ClientID property (if you don't use classes), but you must first attach the parent control to the page for the algorithm to kick in.

So, your code would be something like:

protected Control CreateCommForm()
{
   ...
   column1.Controls.Add(frm);   

   HtmlGenericControl a = new HtmlGenericControl("a");
   a.Attributes["onclick"] = "$('#" + frm.ClientID + "').submit();";
   a.InnerText = "Submit me";
   frm.Controls.Add(a);
   return frm;
}

The alternative way (better separation of concerns)

protected Control CreateCommForm()
{
   ...
   column1.Controls.Add(frm);   

   HtmlGenericControl a = new HtmlGenericControl("a");
   a.Attributes["class"] = "submitter";
   a.InnerText = "Submit me";
   frm.Controls.Add(a);
   return frm;
}

And in javascript we find the parent form and submit it (this can be in a static js file):

<script type="text/javascript">
$('.submitter').click(function( 
  $(this).parents('form').submit();
));
</script>

11 Comments

there are more than form each form created in run time i wanna to submit with specific link button created in run time
can i use your first update in the case i create my forms like i do in my method ??
i wanna the link out side the form because it 's the state
It depends where you are creating the link and how it's being attached to the page. If it's in aspx, use gdoron's code above. If it's in code behind use the CreateCommForm().ClientID to get the client id.
You cannot submit a form to a new window. But you can open the window first, then submit the form.
|

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.