Is there a way to call / run JavaScript from C #?

I have a C # function in which I would like to call / run some JavaScript:

protected void DetailsView1_DataBound(object sender, EventArgs e) {
   ...
   // call/run JavaScript
   ...
}

I am dealing with a form, in particular with submitting a form. After you click Submit, several C # functions are launched that perform validation and other miscellaneous tasks. After that I need to run JavaScript, but I had problems synchronizing these events. JavaScript:

...
if (uploader.total.uploaded == 0) {
   if (uploader.files.length > 0) {
      uploader.start();
   }
   e.preventDefault();
}
...

The way I tried to do this was to detect the click event on the submit button through jQuery and run my Javascript, but then the form itself is not submitted. I tried variations of this, but I had no luck.

, ? "RegisterClientScript" , ? , JavaScript Load ""? , ?

, .

UPDATE

... ​​:

<asp:CommandField ValidationGroup="request" ButtonType="Image" 
        CancelText="Reset" CancelImageUrl="../images/internal/btn_reset.gif"
        InsertImageUrl="../images/internal/btn_insert.gif" ShowEditButton="True" 
        ShowInsertButton="True" />

2 , - "", "".

,

+3
7

jquery .

, .. , - ..

 $(function () {
        $('#form2').submit(function () {
            if ($("#txtServiceTypeCategoryName").val() == "") {
                alert("Please Enter Service Type Category Name");
                return false;
            }
            if ($("#txtServiceTypeCategoryDescription").val() == "") {
                alert("Please Enter Service Type Category Description");
                return false;
            }
            $('#someHiddenDiv1').show();
            $('#CCSave').attr('disabled', 'disabled');
            $('#btnExist').attr('disabled', 'disabled');
            $.ajax({
                url: this.action,
                type: this.method,
                data: $(this).serialize(),
                success: function (result) {
                    $('#someHiddenDiv1').hide();
                    $('#CCSave').removeAttr('disabled');
                    $('#btnExist').removeAttr('disabled');
                    window.location.href = '/ServiceTypeCategory/edit/' + $("#txtServiceTypeCategoryID").val();                  
                    //alert('ServiceTypeCategoryChanges Made Successfully!');
                }
            });
            return false;
        });
    });
0

FAQ:

http://forums.asp.net/t/1360420.aspx#_How_to_register

private void Button2_Click(object sender, System.EventArgs e)
{
    string str;
    str="<script language='JavaScript'>";   
    str+="selectRange()";
    str+="<script>";
    Literal1.Text=str;
}

ASP.NET WebForms , WebForm, # WebForms JavaScript . , . WebForms , - - .

ASP.NET MVC , , JavaScript .. , , , WebForms. MVC - .

+1

. RegisterStartupScript.

Click:

ClientScriptManager scriptManager = Page.ClientScript;
Type type = this.GetType();
string script = "<script type=text/javascript> callMyFunction(); </script>";

scriptManager.RegisterStartupScript(type, "MyName", script);

, , - , " " #, .

, , , .

+1

, , , . "" onclick JavaScript.

document.myform.mybutton.onclick(function() {// });

0

jQuery, "":

$("form").submit(function() {//do something, return true;}

"return true"; make the form file. If you return false (validation completed or something else), the form will not be submitted.

0
source

An example would be:

protected void DetailsView1_DataBound(object sender, EventArgs e)
{
    ...
    // Example JavaScript
    Response.Write("<script language='javascript'> { window.close();}</script>");
    ...
}

I'm not quite sure that this is what you want, but I have used this before.

0
source

As you mentioned, you will want to use RegisterClientScriptBlock or for ajax something like this: ScriptManager.RegisterStartupScript (this, this.GetType (), "MyFunc", "MyFunc ();", true);

0
source

Source: https://habr.com/ru/post/1780744/


All Articles