How to make a linkbutton onclick with parameters - c#

HTML :
<asp:LinkButton ID="lnk_productImage" runat="server" Text="select"
OnClick="viewProductImage('<%#DataBinder.Eval(Container.DataItem,"Id") %>')"
>
</asp:LinkButton>
CodeBehind:
protected void viewProductImage(object sender, EventArgs e, int id)
{
//Load Product Image
}

I see you're using a repeater, so you probably could use this code:
In your repeater template:
<asp:Repeater ID="_postsRepeater" runat="server" OnItemCommand="_postsRepeater_ItemCommand">
<ItemTemplate><asp:LinkButton ID="_postDeleteLinkButton" runat="server" CommandName="DeletePost" CommandArgument="<%# ((Post)Container.DataItem).ID %>">Delete</asp:LinkButton></ItemTemplate>
</asp:Repeater>
Then handle the repeater's ItemCommand event:
protected void _postsRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "DeletePost") // Replace DeletePost with the name of your command
{
// Get the passed parameter from e.CommandArgument
// e.g. if passed an int use:
// int id = Convert.ToInt32(e.CommandArgument);
}
}

Use CommandArgument property of linkbutton to pass parameters.
CommandArgument property:
Gets or sets an optional argument passed to the Command event handler along with the associated command name property.
LinkButton Members

Related

How to get Repeater Item data object on ItemCommand event

I have a repeater of LinkButtons and on the ItemCommand event. I need to get the Data Object that created the link button.
My DataSource is List so on ItemCommand I need MyObject object = ???
Are you looking for this? This will send the ID to the code behind using CommandArgument so it can be processed.
<asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("ID") %>' runat="server" CommandName="myCommand">LinkButton</asp:LinkButton>
And in code behind:
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "myCommand")
{
string myID = e.CommandArgument.ToString();
}
}
Or you can use CommandArgument='<%# Container.ItemIndex %>'. Then you know the row number and can access the corresponding index in your source.
Object DataItem = (Object) e.Item.DataItem;
And then use the DataItem.

codebehind is not setting CssClass on a link button in a repeater

New to webforms/c# I get a reference to the object in the onclick handler.
The reference to the object exists in the Repeater_ItemDataBound method.
After the curObj.CssClass = "XXXXX" has run the class object curObj is updated.
The page renders without the CSS class applied to the object.
I assume this is due to the LinkButton CSS does not apply to the Anchor tag that gets rendered in the end.
So how do I apply the CSS class to the actual rendered Anchor from the code behind?
// my aspx
<asp:Repeater ID="Repeater1" runat="server" onItemDataBound="Repeater_ItemDataBound">
<ItemTemplate>
<asp:LinkButton ID="my_btn" runat="server" OnCommand="cmdSelect_click" CommandArgument='<%# Eval("value") %>'><%# Eval("value") %></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
// my code behind
protected void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (((MyObject)e.Item.DataItem).value == CurrentValue )
{
curObj.CssClass = "someCssClassHere";
}
}
protected LinkButton curObj;
protected void cmdSelect_click(object sender, CommandEventArgs e)
{
curObj = (LinkButton)sender;
CurrentValue = int.Parse(e.CommandArgument.ToString())-1;
}
I dont understand when/where you want to set cssclass..
If you want to set it in ItemDataBound:
protected void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
LinkButton my_btn = (LinkButton)e.Item.FindControl("my_btn");
if (my_btn != null) my_btn.CssClass = "someCssClassHere";
}
or if you want to set after Click:
protected void cmdSelect_click(object sender, CommandEventArgs e)
{
LinkButton my_btn = (LinkButton)sender;
my_btn.CssClass = "someCssClassHere";
}
That's not quite how templated controls such as the Repeater work.
Firstly, you should do a FindControl inside ItemDataBound to find your LinkButton, and apply the CSS to the item that is found.
Secondly, you don't wire up events for controls inside a Repeater that way; instead you handle the ItemCommand event of the Repeater.
Can you post the code you're using to bind the repeater ? It would be useful to know what your DataSource is, then I can post something that works.
This post might help as well - Linkbutton inside Repeater for paging ASP.Net

How to fire an event when dropdown inside Repeater changes

I want to do some actions in my database when user change dropdown's selectedIndex.Now I have the following.
<td class="shop-item-qty">
<asp:DropDownList ID="qtyDropDownList" OnSelectedIndexChanged="changeCount" AutoPostBack="true" runat="server"/>
<asp:HiddenField ID="ItemId" runat="server" Value='<%#Eval("GiftVoucher.ID") %>'/>
</td>
All I want is to get my hidden fields value in changeCount method. The problem is that I can't directly get hidden fields value, because this code is in Repeater element. How can I achieve that functionality?
protected void qtyDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList control = (DropDownList)sender;
RepeaterItem rpItem = control.NamingContainer as RepeaterItem;
if (rpItem != null)
{
HiddenField hiddenField = ((HiddenField)rpItem.FindControl("ItemId"));
}
}
You can bind GiftVoucher.ID value to DropDown's custom attribute and skip HiddenField:
<asp:DropDownList runat="server" ID="qtyDropDownList" OnSelectedIndexChanged="changeCount" AutoPostBack="true" data-itemId='<%# Eval("ID") %>' />
protected void changeCount(object sender, EventArgs e)
{
var id = ((DropDownList)sender).Attributes["data-itemId"];
}

how to find control of dropdownlist inside a datalist

I have a DataList and inside it I have a DropDownList:
<asp:DataList ID="dlconfigureItem" runat="server">
<ItemTemplate>
<asp:DropDownList CssClass="config-select" ID="ddlitem runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:DataList>
How can I get selectedindexchanged event of DropDownList on the server side? I tried this:
public void ddlitem_selectedindexchanged (object sender, EventArgs e)
{
}
but it is not working.
You have defined the server side method:
public void ddlitem_selectedindexchanged (object sender, EventArgs e)
{
}
but you have not told client side that there is an event for you, so in html code tell it like:
onselectedindexchanged="ddlitem_selectedindexchanged"
and also set AutoPostBack property to true.
From the SelectedIndexChanged event the easiest is to cast the sender to the DropDownList
var ddl = (DropDownList)sender;
The sender is always the control that is the source of the event.
For the sake of completeness, from ItemDataBound of the DataList:
protected void dlconfigureItem_ItemDataBound(object sender, DataListItemEventArgs e)
{
DropDownList ddlitem = e.Item.FindControl("ddlitem") as DropDownList;
if (ddlitem != null)
{
// ...
}
}
Edit: Have you forgotten to register the event?
<asp:DropDownList CssClass="config-select"
ID="ddlitem"
OnSelectedIndexChanged="ddlitem_selectedindexchanged"
runat="server">
</asp:DropDownList>
Note that you should not bind your DataList to it's DataSource on postbacks, otherwise events are not triggered. So check for the IsPostBack property of the page.
For example in page_load:
if(!IsPostBack)BindDataList();
Register the event and set AutoPostBack="true"
<asp:DropDownList CssClass="config-select"
ID="ddlitem"
AutoPostBack="true"
OnSelectedIndexChanged="ddlitem_selectedindexchanged"
runat="server">
</asp:DropDownList>
event (on selected index change you can get the selected value)
protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
{
var ddlList = (DropDownList)sender;
string selectedValue = ((DropDownList)ddlList.NamingContainer.FindControl("ddlitem")).SelectedValue;
}
Not sure if you can't get the selected item on the server or you can't find the way to handle the event. In case your problem is with the event handling, try this
<asp:DataList ID="dlconfigureItem" runat="server">
<ItemTemplate>
<asp:DropDownList CssClass="config-select" ID="ddlitem"
OnSelectedIndexChanged="ddlitem_selectedindexchanged"
AutoPostBack="true" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:DataList>

How can I dynamically do write a value into a function call?

<asp:LinkButton class="uibutton normal submit" ID="LinkButtonMAR"
OnClick="MarkClientNoteRead(<%# Eval("NoteID") %>)" runat="server">
Mark as Read</asp:LinkButton>
This is all within a repeater that will return a list of notes. I want to grab the NoteID and throw this into the OnClick="MarkClientNoteRead()" method call.
Is this possible?
You could use the CommandArgument:
<asp:LinkButton class="uibutton normal submit"
ID="LinkButtonMAR"
CommandName="MarkRead"
CommandArgument='<%# Eval("NoteID") %>'
Text="Mark as Read"
OnCommand="MarkClientNoteRead" runat="server" />
protected void MarkClientNoteRead(Object sender, CommandEventArgs e)
{
int NoteID = int.Parse(e.CommandArgument.ToString());
}
Note that you need to handle the Command event instead of the Click event.
Another more convoluted way would be:
<asp:LinkButton class="uibutton normal submit" ID="LinkButtonMAR"
OnClick="LinkButtonMAR_Click" runat="server">Mark as Read
</asp:LinkButton>
<asp:HiddenField ID="NoteID" runat="server" Value='<%# Eval("NoteID")%>' />
Code Behind:
protected void LinkButtonMAR_Click(object sender, EventArgs e)
{
LinkButton thisLinkBUtton = (LinkButton)sender;
// substitute appropriate object for the GridViewRow below
GridViewRow thisGridViewRow = (GridViewRow)thisLinkBUtton.Parent.Parent;
HiddenField hdnNoteID = (HiddenField)thisGridViewRow.FindControl("NoteID");
// run your update code...
}
I've used this, but it bothers me having to use Parent.Parent. Plus, I was needing other bits of data from the same row.

Categories

Resources