I call a javascript function for print but doesn't work - asp.net

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" ....

Related

Button click not firing in UserControl

I have user control deriving from BaseUserControl class and Now I need to add button in UserControl file(ascx)
I have added following code snippet in ascx:
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"/>
Below is the code sample of ascx.cs file
public partial class MyContol : BaseUserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
// code to execute after button is clicked
}
}
But here the button event is not triggering and during debug I am able to see its hitting Page_Load event, please help me to resolve on this.
Regards
Anand
I have seen this type of thing many times. Try the following.
Remove the button from the ASCX side of the page.
Remove the C# btn submit method.
Save the page.
Now go into design view...
Add the button back, assign the name in design view.
In Design View double click on the button so that it created the C# method for you.

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;
}

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

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>

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();
}

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