How to pass data to Link button onclick event - c#

/*help me i am stuck here on how to pass <%# DataBinder.Eval(Container.DataItem, "Site_No") %> as parm to link button when clicked */
<ItemTemplate >
<!-- small box -->
<div class='<%# DataBinder.Eval(Container.DataItem, "Status") %>'>
<div class="inner">
<h3><asp:Label ID="lblSiteNo" runat="server"><%# DataBinder.Eval(Container.DataItem, "Site_No") %><sup style="font-size: 20px"></sup></asp:Label></h3>
<p><%# DataBinder.Eval(Container.DataItem, "Site_Size") %></p>
</div>
<asp:LinkButton ID="LinkButton1" CssClass="small-box-footer" OnClick="" runat="server">More info</asp:LinkButton>
</div>
</ItemTemplate>
</asp:DataList>

Instead of using OnClick event, use the CommandName and CommandArgument properties.
<asp:LinkButton ID="LinkButton1" CssClass="small-box-footer" CommandName="MoreInfo_Command" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "PARAM_NAME") %>' runat="server">More info</asp:LinkButton>
In your code behind:
void MoreInfo_Command(Object sender, CommandEventArgs e)
{
string parameter = e.CommandArgument;
}

Related

Add Repeater items to Listbox

I have a Repeater which contains Eval commands and a Button (Which I can't access) I would like to access this button and when clicked insert one of the Eval commands (Eg. Car Model) into a listbox I have on the same page.
<asp:Button ID="modelButton" CommandArgument='<%# Eval("Model") %>' Text='<%# "Add Model to List:" + Eval("Model") %>' runat="server" />
<asp:Repeater ID="Repeater2" runat="server" DataSourceID="AccessDataSource1" DataMember="DefaultView" OnItemDataBound="Repeater2_ItemDataBound">
<ItemTemplate>
<div>
<p><img src="carImages/<%#Eval("Artwork")%>" /></div>
<div class="col-xs-4">
<h4><%# Eval("Make")%> (<%# Eval("Year") %>)</h4>
<p><%# Eval("Model")%></p>
<p><%# Eval("Colour")%></p>
<p><%# Eval("Type")%></p>
<div>
<asp:Button ID="modelButton" CommandArgument='<%# Eval("Model") %>' Text='<%# "Add Model to List:" + Eval("Model") %>' runat="server" />
</div>
</a>
</div>
</ItemTemplate>
Just add your oncommand argument to the repeated button
<asp:Button ID="modelButton" CommandArgument='<%# Eval("Model") %>' OnCommand="CommandBtn_Click" Text='<%# "Add Model to List:" + Eval("Model") %>' runat="server" />
you can grab the eval in the behind code
void CommandBtn_Click(Object sender, CommandEventArgs e){
var command = e.CommandArgument;
// Do whatever with it here
}
That will get the evaluated model

Get selecteditems value from listview which has datas binded with eval

I bind my data to listview using eval. like that :
<telerik:RadListView ID="lvDSTableSelection" runat="server" AllowMultiItemSelection="true" OnSelectedIndexChanged="lvDSTableSelection_SelectedIndexChanged" EnableViewState="true" >
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" CssClass="selectedButtons" runat="server" CommandName="Select" Font-Underline="false" >
<p ><%# Container.DataItem %></p>
</asp:LinkButton>
</ItemTemplate>
<SelectedItemTemplate>
<p ><%# Container.DataItem %></p>
</SelectedItemTemplate>
</telerik:RadListView>
How can I get selecteditems data at code behind. I tried this in selecteditemchanged event
lvDSTableSelection.SelectedItems[0].DataItem.ToString() ;
When I debugged code I realizde that my SelectedItems[0] count is 1 but SelectedItems[0].DataItem is null. I suppose it is about with usage of eval.
How can I get selecteditems data from codebehind?
I have tried with your code but not able to reproduce this issue.
Please try with the below code snippet.
ASPX
<telerik:RadListView ID="lvDSTableSelection" runat="server" AllowMultiItemSelection="true" EnableViewState="true">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" CssClass="selectedButtons" runat="server" CommandName="Select" CommandArgument='<%# Eval("ID") %>' Font-Underline="false" OnClick="lvDSTableSelection_SelectedIndexChanged">
<p ><%# Container.DataItem %></p>
</asp:LinkButton>
</ItemTemplate>
<SelectedItemTemplate>
<p><%# Container.DataItem %></p>
</SelectedItemTemplate>
</telerik:RadListView>
ASPX.CS
protected void lvDSTableSelection_SelectedIndexChanged(object sender, EventArgs e)
{
string str1 = (sender as LinkButton).CommandArgument;
}

handle asp.net imagebutton in listview

I have a webform that contains a listview like this:
<asp:ListView ID="SlidesListView" runat="server" OnItemCommand="SlidesListView_ItemCommand">
<LayoutTemplate>
<span runat="server" id="itemPlaceholder" style="display: inline-block" />
</LayoutTemplate>
<ItemTemplate>
<div class="SlideDiv">
<div>
<asp:ImageButton ID="ChangeDescriptionImageButton" runat="server" ImageUrl="~/Image/Change.png" CommandArgument="<%# Eval("ID") %>" CommandName="Change" AlternateText="Change" ToolTip="Change" />
<asp:Image ID="Image1" runat="server" CssClass="MiniSliderPic" ImageUrl='<%#Eval("ImagePath") %>' AlternateText='<%#Eval("Description") %>' ToolTip='<%#Eval("Description") %>' />
<p><%#Eval("Description").ToString().Replace(Environment.NewLine, "<br/>").Replace("\n", "<br />") %></p>
</div>
</div>
</span>
</ItemTemplate>
</asp:ListView>
and codebehind:
protected void SlidesListView_ItemCommand(object sender, ListViewCommandEventArgs e)
{
int ID = 0;
switch (e.CommandName)
{
case "Change":
ID = Convert.ToInt32(e.CommandArgument.ToString());
break;
}
}
but when I try to open this webform , I face this error:
An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
the server tag is not well formed
and asp:ImageButton tag get red.why?
Change this:
CommandArgument="<%# Eval("ID") %>"
To this:
CommandArgument='<%# Eval("ID") %>'
BTW, I don't think you can use imagebutton this way. It will fire it's won OnCommand rather than ListView's ItemCommand. Consider using a linkbutton with image inside it.
<asp:LinkButton ID="ChangeDescriptionLinkButton" runat="server"
CommandName="Change"
CommandArgument='<%# Eval("ID") %>' >
<img src="~/Image/Change.png" alt="Change" />Change
</asp:LinkButton>

How to get LinkButton text in nested Repeaters

I have problem getting the LinkButton text in nested Repeaters
<div>
<asp:Repeater ID="rp_resList" runat="server" OnItemDataBound="rp_resList_ItemDataBound">
<ItemTemplate>
<div class="resourcesResult">
<asp:HiddenField ID="hf_resID" runat="server" Value='<%# Eval("Id") %>' />
<a href='<%# Eval("pageID") %>'><%# Eval("name") %></a>
<br />
<asp:Literal ID="litSummary" runat="server" Text='<%# Eval("summary") %>'></asp:Literal>
<br />
<asp:Repeater ID="rp_tagsTopics" runat="server">
<ItemTemplate>
<h6>
<asp:LinkButton ID="lnkBtnTags" runat="server" Text=' <%# Container.DataItem %>' OnClick="LinkButton1_Click" > <%# Container.DataItem %></asp:LinkButton>
</h6>
</ItemTemplate>
</asp:Repeater>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton lnkBtnTags = (LinkButton)rp_tagsTopics.FindControl("lnkBtnTags");
Response.Redirect("~/WebsofWonder.aspx?tag=" + lnkBtnTags.Text);
}
Or you can make use of the ItemCommand event by specifying the CommandName and CommandArgument parameters of the LinkButton
<asp:LinkButton ID="lnkBtnTags" runat="server" Text=' <%# Container.DataItem %>' OnClick="LinkButton1_Click" CommandName="Redirect" CommandArgument='<%# Container.DataItem %>' > <%# Container.DataItem %></asp:LinkButton>
And in the handler use the parameters:
protected void rp_tagsTopics_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
if( e.CommandName == "Redirect" )
{
Response.Redirect("~/WebsofWonder.aspx?tag=" + e.CommandArgument);
}
}
What you should do is use the sender argument in the LinkButton_Click handler to get access to the instance of LinkButton that was actually clicked, and has raised the Click event:
protected void LinkButton1_Click(object sender, EventArgs e)
{
// Use sender instead of trying to find the control within the Repeater
LinkButton lnkBtnTags = (LinkButton) sender;
Response.Redirect("~/WebsofWonder.aspx?tag=" + lnkBtnTags.Text);
}

How to to get hiddenfield value in ItemTemplate

Just want to ask how can I find hiddenfield in repeater because my problem I have button and I want to get the associate hiddenfield inside ItemTemplate because I get null value when I try to get hiddenfield value
<asp:Repeater ID="rp_resList" runat="server" OnItemDataBound="rp_resList_ItemDataBound">
<ItemTemplate>
<div class="resourcesResult">
<asp:HiddenField ID="hf_resID" runat="server" Value='<%# Eval("Id") %>' />
<a href='<%# Eval("pageID") %>'><%# Eval("name") %></a>
<br />
<asp:Literal ID="litSummary" runat="server" Text='<%# Eval("summary") %>'></asp:Literal>
<br />
<%-- <asp:Repeater ID="rp_tagsSkill" runat="server">
<ItemTemplate>
<h6>
<%# Eval("Description") %>
</h6>
</ItemTemplate>
</asp:Repeater>--%>
<asp:Repeater ID="rp_tagsTopics" runat="server">
<ItemTemplate>
<h6>
<%# Eval("Description") %>
</h6>
</ItemTemplate>
</asp:Repeater>
<div id="controls">
<asp:ImageButton ID="imgbtnBookmark" runat="server" OnClick="imgbtnBookmark_Click" />
<asp:DropDownList ID="ddlGroup" runat="server" DataSourceID="SqlDS_Groups" DataTextField="name" DataValueField="id" AppendDataBoundItems="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" >
<asp:ListItem Value="-1">Select Group</asp:ListItem>
protected void imgbtnBookmark_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
Bookmark bm = new Bookmark();
HiddenField hiddenField = rptGroup.FindControl("hf_resID") as HiddenField;
bm.UserID =
Guid.Parse(Membership.GetUser(HttpContext.Current.User.Identity.Name).ProviderUserKey.ToString());
bm.Resoursce.ResourceID = Convert.ToInt32(hiddenField.Value);
Bookmark.Insert(bm);
}
Try this, change you button tag to pass id as CommandArgument value
<asp:ImageButton ID="imgbtnBookmark" runat="server"
OnClick="imgbtnBookmark_Click"
CommandArgument='<%# Eval("Id") %>'/>
in your button click event you can access id using
bm.Resoursce.ResourceID = Convert.ToInt32(e.CommandArgument.ToString());
Change rptGroup.FindControl("hf_resID") as HiddenField; to
e.Item.FindControl("hf_resID")....
I hope this link will help you .
jquery
function showid(dllval) {
var ID = $(dllval).parent().parent().find('[id*="hiddenID"]').val();
alert(ID)
}
asp.net
<asp:DropDownList ID="ddl" runat="server" onclick="showid(this);" >
</asp:DropDownList>
<asp:HiddenField ID="hiddenID" runat="server" Value='<%#Eval("ID")%>' />

Categories

Resources