How to pass parameters to click event method using c# - c#

i have this button on my C# context:
<button id='Update' OnServerClick='UpdateCart'>Update</button>
now I wanna to pass parameter (num) to (UpdateCart) Method :
protected void UpdateCart(object sender, EventArgs e)
{
Response.Cookies["Cart"]["Qty(" + num + ")"] = Request.Form["Qty" + num];
}
How I can do that ?

Use ASP.NET Button control instead of <button/> markup that allow you to set value via CommandArgument property and use Command event (do not use Click) to retrieve value of CommandArgument property.
Markup:
<asp:Button
ID="Button1"
runat="server"
CommandArgument="10"
oncommand="Button1_Command"
Text="Button" />
Code:
protected void Button1_Command(object sender, CommandEventArgs e)
{
string value = e.CommandArgument.ToString();
}

You would use the 'commandArgument' attribute. Here's an example on the MSDN
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandargument.aspx

I noticed that command argument does not accept dynamically set values from client side, command argument must be set server side. So to combat this I used asp:hidden field updated its value and then from code behind got the value by just doing hiddenfield.value.

Related

How can I pass data from serverside to clientside

I read data from database in sreverside (for example a name) and I want to use this name in clientside. But I don't pass the name to clientside(in javascript method). How can I achieve this?
(using asp.net)
using HiddenField:
serverside:
protected void callbackPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
{
hiddenFieldGenel.Add("license", "license1");
}
clientside:
<dx:ASPxHiddenField ID="hiddenFieldGenel" runat="server" ClientInstanceName="hiddenFieldGenel">
</dx:ASPxHiddenField>
function saveClick(s, e) {
var licence = hiddenFieldGenel.Get('license');
}
If its just a single value, you can use Hidden variable:-
<asp:HiddenField ID="NameHidden" runat="server" ClientIDMode="Static" />
And you can simply access it using Javascript or jQuery:-
$('#NameHidden').val();
document.getElementById('NameHidden');

Pass an asp web control as a CommandArgument

In my web app I have a series of ImageButtons. Upon clicking any of these ImageButtons, the ImageButton that was clicked needs to have it's image changed.
I would like one OnClick method that performs its action on the ImageButton that was clicked. Here is my attempt so far:
ASP.NET Code:
<asp:ImageButton ID="ImageToChange1" runat="server" OnClick="ChangeImage" CommandArgument="ImageToChange1"/>
<asp:ImageButton ID="ImageToChange2" runat="server" OnClick="ChangeImage" CommandArgument="ImageToChange2"/>
<asp:ImageButton ID="ImageToChange3" runat="server" OnClick="ChangeImage" CommandArgument ="ImageToChange3"/>
For my C# Code I want to do something like the following:
public void ChangeImage(object sender, CommandEventArgs e)
{
e.CommandArgument.ImageUrl = "~/Images/Penguins.jpg";
}
I'm wondering if there is a more appropriate way to do this than using the "OnClick" event or if I can accomplish this by manipulating "e" or "sender" somehow. Any insight would be greatly appreciated. Thank you.
In your ChangeImage, you can get the CommandArgument and the control as below
public void ChangeImage(object sender, CommandEventArgs e)
{
ImageButton imageButton = sender as ImageButton;
string imageToChange = e.CommandArgument;
//Then you can assign the appropreate image
imageButton.ImageUrl = "~/Images/Penguins.jpg";
}

Click Event of Dynamically generated Anchor Not Firing

I have an anchor on Lable Text.I am creating anchor with runat="server" dynamically on click of a button, It gets created as expected. I want to use its click event but it does not fire.
My code :
lblEmail.Text = email + " <a href='#' runat='server' class='crossicon' onclick='removebtn_Click'></a> ";
protected void removebtn_Click(object sender, EventArgs e)
{
}
How Can I create event for this button?I don't want to use JS, as in that case I will have to use a hidden field for new value
Adding markup/text to the label in that way wouldn't add the linkbutton or register the event to the control tree at the server. For that behavior of dynamically adding controls along with the server events to be achieved, you need to register the controls and events (as shown below)
aspx:
<asp:Panel runat="server" id="pnlEmail">
<asp:Label runat="server" id="lblEmail"/>
</asp:Panel>
aspx.cs:
In whichever event, you want to set the label text (along with the link)
lblEmail.Text = email;
LinkButton lnkbtnEmail = new LinkButton();
lnkbtn.Click += lnkbtn_Click;
lnkbtn.Text = "Dynamic Link";
pnlEmail.Controls.Add(lnkbtnEmail);
And the Handler would be
void lnkbtn_Click(object sender, EventArgs e)
{
// code for your dynamically generated link
}
By default, controls use __doPostBack to do the postback to the server. __doPostBack takes the UniqueID of the control (or in HTML, the name property of the HTML element). The second parameter is the name of the command to fire.
<a href='#' runat='server' class='crossicon' href="javascript:void(0);" onclick="__doPostBack('someuniqueid', '');></a>
System.Web.UI.Page already implements the IPostBackEventHandler interface by default, so you don't need to implement it on every page - it's already there.
You can override the RaisePostBackEvent method on the page like this:
protected override void RaisePostBackEvent(IPostBackEventHandler source, string eventArgument)
{
//call the RaisePostBack event
base.RaisePostBackEvent(source, eventArgument);
if (source == SomeControl)
{
//do something
}
}
I hope it will help you.
I don't think so it is created dynamically. Where is ID of that control. You have to Create Server side control and add it into some placeholder/panel e.t.c

Input field to populate GridView

I have the following code which defined my input field and my button:
<p>
<asp:TextBox ID="input" runat="server"></asp:TextBox>
<asp:Button ID="submit" runat="server" OnClick="submit_Click" Text="Button" />
</p>
<p>
<asp:GridView ID="searchDisplay" runat="server" OnSelectedIndexChanged="searchDisplay_SelectedIndexChanged">
</asp:GridView>
</p>
I added the a Click event to my Button once pressed it should invoke the following method:
protected void submit_Click(object sender, EventArgs e)
{
String keyword = input.Text; // Gets text inputed
List<T> = service.getTitles(keyword); // Two problems here
}
I have 2 problems i need to solve:
1) List should be defined as List<Publication> however type Publication is not part of this solution so i dont know how to correctly bind the data that WCF service returns to it. I tried E, T, and ? as parameters to List but all i get are errors.
Solved
2) I have a WCF service runing in another VS2012 instance how and in the first one i have it refrenced with keyword "service" why can't i access it's methods. I have a method in my WCF service that returns List<Publication> i tried writing List<?> data = service.getTitles(keyword); however this gives an error that the following is a type not valid in current context
you can try the following
protected void submit_Click(object sender, EventArgs e)
{
String keyword = input.Text; // Gets text inputed
var v= service.getTitles(keyword);
grd.dataSource=v;
grd.dataBind();
}

SharePoint - Passing Dataview Row Arguments to a C# function from ASP.net button

I've done this with a asp.net Gridview. but does anybody have any examples on how to pass row information from an asp.net button in SharePoint Dataview rows to an inline C# function?
the button Repeats as follows:
<asp:Button runat="server" Text="Delete" id="Delete{#ID}" OnClick="DeleteDoc()"/>
My function looks like this:
void DeleteDoc(object sender, EventArgs e) { }
}
I tried adding another parameter but get:
No overload for 'DeleteDoc' matches delegate 'System.EventHandler'
Try to change your method access modifier to protected and OnClick="DeleteDoc()" to OnClick="DeleteDoc".
Also i didn't managed to make it work with id="Delete{#ID}" so try without that to.
This worked on my test case
<asp:Button runat="server" Text="Delete"
OnClick="DeleteDoc"
CommandArgument='<%# Eval("ID")%>'/>
and
protected void DeleteDoc(object sender, EventArgs e)
{
Button b = sender as Button;
YourDeleteDocumentHelperMethod(b.CommandArgument);
}

Categories

Resources