Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In Asp.net c# on button click event(server side) I need to clear all textbox on the form using javascript method on add button click and then I need to set value to textbox by serverside as given bellow.
But it is not working if I comment registering script line then it working but I also need to clear the textboxes.
C#
protected void btnAdd_Click(object sender, EventArgs e)
 {  
    ScriptManager.RegisterStartupScript(this, typeof(string), "key1","ClearControls();",                    true);
    TxtChallanNo.Text = "New";
  }

Using above code after add button click event textbox remain empty, I expect to set it to “New”
Thanks in advance.
Posted
Updated 2-Sep-15 19:02pm
v2
Comments
Schatak 3-Sep-15 1:07am    
You means after calling ClearControls() function from Javascript, you need to set txtChallanNo text box's value to "New"?
PatilAnil 3-Sep-15 1:10am    
yes exactly
Sergey Alexandrovich Kryukov 3-Sep-15 1:14am    
What prevents you from writing the value you want?
—SA
Schatak 3-Sep-15 1:17am    
then what is the issue? are you not getting the value?
PatilAnil 3-Sep-15 2:26am    
after calling javascript function clearcontrols() when i set values to textbox on that form by server side
ie.

btnAdd_click()
{
clearcontrols();//----Javascript Method called using registerstartupscript
TxtID.Text=GetNextID();//----server side method to get next id(max +1) // I have set "New" in my example
TxtDate.Text= "03-Sept-2015";//Simply assinged some text
}

i m not able to see the result of last two line why ? If i comment first line then it works properly but i also need to clear controls only by javascript.

You have to assign TxtChallanNo.Text = “New”; into your javasScript function i.e. ClearControls(); your executing code wrongly because when you call the following line

ScriptManager.RegisterStartupScript(this, typeof(string), "key1","ClearControls();", true);

it does not execute it that time itself when your button click execution completed that time it execute your javascript code. For more information about this please insert breakpoint on button click as well as javascript function then check how your code get executed.
 
Share this answer
 
There are many ways to call JS function in code behind, one of them is:
C#
protected void MyButton_Click(object sender, EventArgs e)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "myScript",           "SomeJSFunction();", true);
        textID.Text =SomeFunction();
        textName.Text = "SomeRandomeText";
    }
    public String SomeFunction()
    {
        count += 1;
        return count.ToString();
    }

Try this , it will work for you.
Thanks
 
Share this answer
 
v2
Comments
F-ES Sitecore 3-Sep-15 4:33am    
There are no ways to call js in code-behind. Such statements add to people's confusion about the client\server model. When doing asp.net there are basic concepts that are vital to understand such as the server only generating html which is sent to the browser to be executed. When people think they are calling js from their server code it makes them attempt to implement solutions that will never work. Such as the one being tried by the OP.
Schatak 3-Sep-15 4:37am    
i do understand the risk of it. but if there are such functions by asp.net which allow us to call as per our need, then its not bad to use it.
Yes it is advisable not to use such approaches in avoidable circumstances.

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