How i can call JavaScript function from asp.net server side if script in user control - asp.net

I have a ASP.NET page with 1 user controls registered.
i have one server control
<asp:LinkButton ID="vidoUpdatebtn" OnClick="vidoUpdatebtn_Click" runat="server">LinkButton</asp:LinkButton>
in .cs i handle
protected void vidoUpdatebtn_Click(object sender, EventArgs e)
{
//do something
ScriptManager.RegisterStartupScript(this, this.GetType(), "myscript", "doSomeThing()", true);
}
in user control i have function doSomeThing()
function doSomeThing() {
alert("TestReady");
}
when i click on LinckButon registred function dosen't work

Write this in PageLoad (the only modifications from what you were writing are the moment in time of the registration -- PageLoad not Click, Click is too late and the face that you must write the actual implementation of the javascript function here):
protected void Page_Load(object sender, EventArgs e) {
ScriptManager.RegisterStartupScript(this, this.GetType(), "myscript",
#"function doSomeThing() {
alert(""TestReady"");
}"
, true);
}
and this in the asp:LinkButton tag (you must specify the OnClientClick attribute):
<asp:LinkButton ID="vidoUpdatebtn" runat="server" OnClick="vidoUpdatebtn_Click" OnClientClick="doSomeThing()" >LinkButton</asp:LinkButton>
And it should work.

Have you tried this way:
Add Script manager on your aspx page
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
Codebehind:
Page.ClientScript.RegisterStartupScript(this.GetType(),"myscript", "doSomeThing()",true);

If you want client side code to run on the button click, you could use the OnClickClick property instead of the OnClick event. That'll also avoid the need for a postback and will give immediate feedback to the user.
e.g.
<asp:LinkButton ID="vidoUpdatebtn" OnClientClick="alert('TestReady'); return false;" runat="server">LinkButton</asp:LinkButton>

Related

ASP.NET passing variables from web form to code behind

i'm developing a web application for my company, but i'm coming up against a very simple problem that i don't now how to resolve. I search a lot on internet but i can't find anything.
I need to call a code behind method from an asp.net controls passing variables.
This is the method in the code behind (file.aspx.cs):
protected void SayHello(object sender, EventArgs e, String RandomName)
{
Response.Write(PrintedString);
}
And this is a asp.net control that call the method through OnLoad event :
<asp:Label ID="Label1" runat="server" Text="Hello" OnLoad="Visibility('ciao mamma')"></asp:Label>
What's wrong with this simple thing? Where i'm wrong?
Please answer to this simple question, it's driving me crazy...Thanks.
You can pass an argument to the event handler by the following way, but please note it will work for only asp button.
<asp:Button ID="btn" runat="server" Text="Click" OnCommand="btn_Command" CommandArgument="YourArgumentValue"/>
and then in the code behind file you do like this :
protected void btn_Command(object sender, CommandEventArgs e)
{
Response.Write(e.CommandArgument);
}
It will work for only asp button because they have a onCommand attribute with them.
whenever you will click on the button this code behind file code gets executed.
Hope this helps.
You can set the value to label on Page_Load event
You can use below code.
protected void Page_Load(object sender, EventArgs e)
{
string UserName="test";
Label1.Text="Hello "+ UserName;
}
Is this what you need?
Markup:
<asp:Label ID="lbl" runat="server"><%=SayHello("foo") %></asp:Label>
Code behind:
protected string SayHello(string name)
{
return "Hello " + name;
}

I call a javascript function for print but doesn't work

I use below code for print from Page but doesn't work (means when i click on print button nothing happen). the function doesn't call
<script type="text/javascript">
function printing() {
window.print();
}
</script>
protected void print_Click(object sender, EventArgs e)
{
btnPrint.Attributes.Add("onclick", "return printing()");
}
Try this way:
<asp:Button ID="print" runat="server" Text="Print" OnClientClick="javascript:window.print();" />
Add attribute in page_load event to bind the javascript event, so that the javascript is binded before you click the print button to print the page.
private void Page_Load(object sender, System.EventArgs e)
{
btnPrint.Attributes.Add("onclick", "return printing()");
//Your code here.
}
When a user clicks the button, you are adding an attribute, not calling the printing() function.
You should add the OnClientClick attribute to the button in the button html like this:
<asp:button OnClientClick="return printing" ....

How to get Postback Data on LinkButton Click Event?

I have a LinkButton in aspx page.
<asp:TextBox ID="textBoxNote" runat="server" />
<asp:LinkButton ID="linkButtonUpdateNote" Text="Update" OnClick="ButtonUpdateNoteClicked" runat="server" />
the click event handler has the following code
protected void ButtonUpdateNoteClicked(object sender, EventArgs e)
{
var note = textBoxNote.Text;
}
On Postback textBoxNote.Text is empty. I don't get the posted value. How to get the value?
It seems like you are possibly resetting the value in your Page_Load.
Check that you are using IsPostback check in the Page_Load function. see - http://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback.aspx
private void Page_Load()
{
if (!IsPostBack)
{
DoThisOnce();
}
DoThisOnEachPostback();
}

Can I put a Timer in Ajax updatePannel in a masterpage?

I made a very simple pannel updating wich will query a database and show a message on masterpage through a label.
Thus, I've put an updatePannel with a label and a timer within (obviously with a scriptManager) in my site's masterpage.
However, when I try to interact with the timer1 object, I receive an error message: "object not set to an instance of an object". I not receive this message when placing the schema in a blank page (without masterpage).
I must to run the query in masterpage because the users need to receive information whatever they are in my site.
How can I correctly place the components to do this work? what I'm doing wrong?
Thanks!
I just tried it within a master page without a problem. Here is the relevant code:
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" />
<asp:UpdatePanel ID="updMessage" runat="server">
<ContentTemplate>
<asp:Label ID="lblMessage" runat="server" />
<asp:Timer ID="tmrMessage" Interval="5000" ontick="tmrMessage_Tick" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
From master page code-behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["Message"] = 1;
}
}
protected void tmrMessage_Tick(object sender, EventArgs e)
{
int message = (int)Session["Message"];
lblMessage.Text = message.ToString();
Session["Message"] = ++message;
}
The problem was where I was putting the panel, label and timer. I put in contentPlaceHolder, where the problem occours. Now I put the control in the form tag, then the problem is solved.

Mulitple linkbuttons in user control needs to call event handler in the aspx page

I have multiple user controls on a page and each of them have multiple linkbuttons that perform the same logic on the server side. Is there a way for all the linkbuttons to have the same event handler that is defined in the code behind of the page?
If needed, I can change the linkbuttons to be any other HTML or ASP.NET control as long as it can support a clickable image.
Inside your usercontrol create an event and wire it up. Then call it from the linkbuttons OnClick event handler:
UserControl.ascx:
<asp:LinkButton
runat="server"
id="linkbutton1"
OnClick="LinkButtonClicked">
Click Me
</asp:LinkButton>
<asp:LinkButton
runat="server"
id="linkbutton2"
OnClick="LinkButtonClicked">
Click Me Again
</asp:LinkButton>
UserControl.ascx.cs
public event EventHandler UserControlLinkClick;
protected void LinkButtonClicked(object sender, EventArgs e)
{
if (this.UserControlLinkClick!= null)
{
this.UserControlLinkClick(this, e);
}
}
Inside your parent page wire up the user controls UserControlLinkClick:
protected override void OnInit(EventArgs e)
{
MyUserControl uc = LoadControl("~/PathToUserControl.ascx");
uc.UserControlLinkClick += new EventHandler(MyUserControl_UserControlLinkClick);
}
protected void MyUserControl_UserControlLinkClick(object sender, EventArgs e)
{
//this code will execute when the usercontrol's LinkButtonClicked event is fired.
}
Hope that helps!!
Create an Event/Delegate in your custom control. Then in your page code behind, you can add event handlers or subscribe methods to the delegate.
Link for Events
Link for Delegates
Hope this helps

Resources