Why Link Button control variable doesn't get any value? - c#

I created LinkButton that located inside of Repeater Control.
CategoryID is a variable in LinkButton Control that have to get value after Repeater Control was bound to data. But CategoryID always get zero.
I have the following ASP and C# code:
<asp:Repeater ID="rpt1" runat="server"
OnItemDataBound="rpt1_ItemDataBound"
OnItemCommand="rpt1_ItemCommand">
<ItemTemplate>
<div>
<%# Eval("Name") %>-<%# Eval("CollectionType")%>
<asp:LinkButton ID="LinkButton1" runat="server" Text="[edit item]"
PostBackUrl='AddItem.aspx?CategoryID=<%# Eval("CollectionID")%>' />
</div>
</ItemTemplate>
</asp:Repeater>
Code behind:
public void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<GlassesCollection> gc = BL.GetDataBL.GetCollection();
rpt1.DataSource = gc;
rpt1.DataBind();
}
}
Any idea why CategoryID variable doesn't get any value and how can I fix the problem?

A server control parameter cannot contain a mixture of literal text and evaluated expressions.
The code you have will literally be posting back to AddItem.aspx?CategoryID=<%# Eval("CollectionID")%> and it will not be evaluating the code within the angle brackets.
You need to change your parameter like so
PostBackUrl='<%# "AddItem.aspx?CategoryID=" + Eval("CollectionID")%>' />

Related

Pass variable value from aspx link button to code behind in ASP. NET using C#

Inside a for loop, how to pass the variable to the code-behind from an asp tag and access the variable value
CommandArgument works but shows <# gigs[x].Id%> - not the value.
Aspx
<%for (int x = 0; x < gigs.Count; x++){%>
<asp:LinkButton ID="LinkButton2" CssClass="btn btn-danger mt-3" runat="server"
UseSubmitBehavior="false" CommandArgument="<# gigs[x].Id%>"
OnClick="LinkButton2_Click">Hide Gig <i class="fa fa-eye-slash ml-1"></i>
</asp:LinkButton>
Code behind
protected void LinkButton2_Click(object sender, EventArgs e)
{
LinkButton lnk = sender as LinkButton;
String Value1 = lnk.CommandArgument;
Response.Write(Value1);
}
Result
<# gigs[x].Id%>
Expected Result
1
Try using <%# gigs[x].Id %> instead and call Page.DataBind() on the Page_Load event.
<%= %> is a shortened response.Write() and is never valid as an attribute, for any server tag.
<%# %> can be used, only if the container is databound (the page in your case).
You're much better off using a Strongly Typed Repeater. You can set the ItemType as your class gigs and have full access to all it's properties. Then you can easily use them in the LinkButton.
Repeater1.DataSource = gigs;
Repeater1.DataBind();
And then in the aspx
<asp:Repeater ID="Repeater1" runat="server" ItemType="MyNameSpace.MyClass.Gig">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" CommandArgument='<%# Item.Id %>' OnClick="LinkButton2_Click" runat="server">Hide Gig</asp:LinkButton>
</ItemTemplate>
</asp:Repeater>

Put variables and statements into text of a button

I'm trying to figure out how to put something else than just static string into text attribute of <asp:button>. Following code:
<asp:Button runat="server" ID="updateList" Text=<%= isEditing ? Resources.Labels.Update : Resources.Labels.Insert %> />
returns
Parser Error Message: Server tags cannot contain <% ... %> constructs.
You need a Databinding expression for that <%# %>:
<asp:Button runat="server" ID="updateList" Text='<%# isEditing ? Resources.Labels.Update : Resources.Labels.Insert %>' />
But for it to work outside a GridView, Repeater etc you have to call DataBind() manually.
protected void Page_Load(object sender, EventArgs e)
{
DataBind();
}

Repeater datasource Connection not working

Here is my Code Structure.
Repeater Markup:
<asp:Repeater runat="server" ID="RPMenu" DataSource='<%# Menues.GetAllMainMenu() %>'>
<ItemTemplate>
<%# Eval("MenuName") %><br />
<asp:Repeater runat="server" ID="RPMenuUnder" DataSource='<%# Menues.GetAllMainMenu(Convert.ToInt32(Eval("MenuID"))) %>'>
<ItemTemplate>
<%# Eval("MenuName") %><br />
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
Menu Class :
public static List<Menu> GetAllMainMenu(int parrentID = 0)
{
using (Scooterfrøen_Entities db = new Scooterfrøen_Entities())
{
return db.Menu.Where(i => i.ParentMenuID == parrentID).ToList();
}
}
Database table:
MenuID | MenuName | MenuDescription | ParrentMenuID | MenuUrl
I have several rows where ParentMenuID IS 0 and NOT NULL.
But for some reason Repeater control does not list anything on the site.
What could be the reason why Repeater control don't show anything?
I think page still requires to bind even though you have provided datasource in markup.
Write Page.DataBind(); in your page load event:
protected void Page_Load(object sender, EventArgs e)
{
Page.DataBind();
}
How to Use nested Repeater:It seems you have to handle ParentRepeater_ItemDataBound event to bind child repeater control.
Checkout this article available here.
You have to set values and bind repeater
RPMenu.DataSource=<value you want to set>;
RPMenu.DataBind();
A combination of the two existing answers is closest to true.
Rather than run Page.DataBind(), which may have unintended side-effects, run RPMenu.DataBind() in the Page_Load event handler. It will use the DataSource which is set in code-front.
The inner Repeater should automatically be bound. You only need to explicitly databind the outermost Repeater.

findcontrol in listview

I want to use FindControl to find the value of the HiddenField i.e hfBlogID. I want to find the value on a ButtonClick
<asp:ListView ID="lvArticle" runat="server">
<LayoutTemplate>
<div runat="server" id="itemPlaceHolder">
</div>
</LayoutTemplate>
<ItemTemplate>
<asp:HiddenField ID="hfBlogID" Value='<%#Eval("BlogID")%>' runat="server" />
<p>
<%#Eval("BlogTitle")%></p>
<p>
<%#Eval("BlogDetails")%></p>
</ItemTemplate>
</asp:ListView>
In order to determine the correct row index you should place your button inside of your ListView.ItemTemplate and handle the ListView.ItemCommand event.
In order to implement this approach you would have to change your code as follows:
<asp:ListView ID="lvArticle" runat="server" OnItemCommand="lv_ItemCommand">
..
<ItemTemplate>
<asp:HiddenField ID="hfBlogID" Value='<%#Eval("BlogID")%>' runat="server" />
<p>
<%#Eval("BlogTitle")%></p>
<p>
<%#Eval("BlogDetails")%></p>
<asp:Button runat="server" CommandName="find" CommandArgument='<%# Eval("yourIDField") %>' />
</ItemTemplate>
...
In code behind:
protected void lv_ItemCommand(object sender, ListViewCommandEventArgs e)
{
switch (e.CommandName)
{
case "find":
var hidden = e.Item.FindControl("your hidden id") as HiddenField;
break;
}
}
If your button is not inside your ListView, then you would need a way to identify the row you want to extract the hidden value from.
For example, if you allow to select a row in your ListView then you could get the hidden value from the selected row as follows:
protected void find_Click(object sender, EventArgs e)
{
var hidden = this.lv.Items[this.lv.SelectedIndex].FindControl("your hidden ID") as HiddenField;
}
If the button is in the same item template then use ItemCommand event handler and in that handler you can fetch the hidden field directly.
If the button is outside of list view then you need to get the index of item whose hidden field'd value you want to get.
you can use if button is in your listview
var control = (HiddenField)e.Item.FindControl("hfBlogID");
or if button is not in your listvew
var contorl = (HiddenField)this.lvArticle.Items[this.lvArticle.SelectedIndex].FindControl("hfBlogID");
Here you can access the hidden field for each item:
protected void Button1_Click(object sender,EventArgs e)
{
foreach(ListViewDataItem item in lvArticle.Items)
{
HiddenField hf=(HiddenField)item.FindControl("hfBlogID");
}
}
if you have index of item already then you can get it directly like this
HiddenField hf=(HiddenField)lvArticle.Items[index].FindControl("hfBlogID");
Hope this will help..

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