find current element in repeater - c#

How can i get current MainNavigationMenu hyprelink in code behind and check if is current menu clicked then i will change him default CSS.
I try with this code but is always null
HyperLink mainNavigationMenu = siteMapAsBulletedList.FindControl("MainNavigationMenu") as HyperLink;
full repeater code:
<asp:Repeater runat="server" ID="siteMapAsBulletedList" DataSourceID="smdsMenu">
<HeaderTemplate>
<li><asp:HyperLink ID="MainNavigationMenu" runat="server" NavigateUrl='<%#SiteMap.RootNode.Url%>'
Text='<%#SiteMap.RootNode.Title%>'></asp:HyperLink></li>
</HeaderTemplate>
<ItemTemplate>
<li><asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#Eval("Url")%>' Text='<%#Eval("Title")%>'></asp:HyperLink></li>
</ItemTemplate>
</asp:Repeater>

Are you verifying which type of repeater item you are looking at ?
Attach an ItemDataBound to your repeater and do something like this :
private void rptPanier_ItemDataBound(Object sender , RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
var myItem = (Hyperlink)e.Item.FindControl("YourControlName");
}
}
Then you will have a reference on it and you can do whatever you want !
Beware tho, the ItemData item, which can be found in the repeateritemeventargs, is always null when the repeater is creating the header.
Hope this helps !

The repeater has a collection of Items. Each Item is a RepeaterItem, which has an ItemType property. For header items, this value will be "ListItemType.Header". Therefore, you want to perform .FindControl() upon that particular repeater item, not the entire Repeater itself.

Related

getting GridView data from buttonField press?

So I have a GridView for a C# web application, that has a Buttonfield, and when that button is clicked, I need to get the value of one of the fields for that row and store it in a variable for processing in some way.
However, neither the GridView nor the ButtonField seem to possess any means of doing this.
Can anyone recommend a way of getting data from a GridView, or if this is not possible, a different type of view that does offer this functionality, while still displaying a whole table (eg, not a DetailsView)
You can Check this link: https://msdn.microsoft.com/en-us/library/bb907626(v=vs.140).aspx.
Define the CommandName of the Button.
In the GridView Define the RowCommand Event and Check the CommandName.
Get the Index of the Row.
Get the Column with GridView.Rows[index](columnIndex)
If you are using asp:TemplateField like shown below then you can access the row content using RowCommand
Markup
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblCode" runat="server" Text='<%# Eval("CustomerID") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField CommandName="AddCode" Text="Add New"/>
Code
protected void gvwSearch_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName == "AddCode")
{
var clickedButton = e.CommandSource as Button;
var clickedRow = clickedButton.NamingContainer as GridViewRow;
var rows_lblCode = clickedRow.FindControl("lblCode") as Label;
// now you can acccess all the label properties. For example,
var temp = rows_lblCode.Text;
}
}

Get dynamic controls and text in Repeater

I have a Repeater which loads some data from a SQL database
<asp:Repeater ID="Repeater" runat="server" OnItemDataBound="Repeater_ItemDataBound">
<ItemTemplate>
<asp:Label ID="QuestionLabel" runat="server" Text=""></asp:Label>
<asp:TextBox ID="AnswerLabel" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:Repeater>
<asp:Button ID="AnswerSubmit" runat="server" Text="Insert"/>
In code behind, i assign to get the questions on another button click to load and bind the Repeater. In ItemDataBound i find the controls and assign the questions to the label.
How should i get the answers that the user enters and store the ID of the question with the answer they enter in a button click event?
At first, I tried this in the button click event
foreach (RepeaterItem item in Repeater.Items)
{
Label QuestionLabel = (Label)item.FindControl("QuestionLabel");
}
but it couldnt find the QuestionLabel. Looking at the page source i believe thats due to the label having a different value (QuestionLabel_0, QuestionLabel_1 etc), so struggling to find a way to approach this?
Edit
protected void QuestionButton_Click(object sender, EventArgs e)
{
Repeater.DataSource = QuestionsByID(ID);
Repeater.DataBind();
}
You see
foreach (RepeaterItem item in Repeater.Items)
{
if (item.ItemType == ListItemType.Item
|| item.ItemType == ListItemType.AlternatingItem)
{
Label QuestionLabel = (Label)item.FindControl("QuestionLabel");
}
}

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.

get value from textbox within repeater asp.net c#

I've been trying to get this working for a couple of hours now but nothing from google could help me fix the problem.
I have a very simple repeater control:
<asp:Panel ID="userDefDiv" Visible="false" runat="server">
<asp:Repeater ID="userDefRepeater" EnableViewstate="false" runat="server">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" EnableViewState="false"></asp:TextBox><br/>
</ItemTemplate>
</asp:Repeater>
</asp:Panel>
the userDefDiv panel is inside another panel, which is inside contentPLaceHolder.
the parent panel to userDefDiv does NOT have the "enableviewstate="false"".
So.
Everything on this page happens after a couple of linkbuttons_click. so nothing happens during page_load. And after i click another linkbutton i want to get the data from the different textboxes that is within the repeater.
C# code:
This is the code to create all the repeater items.
public void createUserDef()
{
DataTable userDefData;
userDefData = ..... (data from Database.)
userDefDiv.Visible = true;
userDefRepeater.DataSource = userDefData;
userDefRepeater.DataBind();
}
The code for the linkbutton:
protected void linkButton_Click(object sender, EventArgs e)
{
createUserDef();
Label2.Visible = true;
foreach (RepeaterItem item in userDefRepeater.Items)
{
TextBox box = (TextBox)item.FindControl("TextBox1");
string b = box.Text;
Label2.Text += b + " . ";
}
}
As you see i create the repeater once again during the click. But the only thing i can read in label2. is a a number of " .", on dot for each textbox.
but the text from the textbox is empty..
What am I doing wrong??
thanks for reading!
Mattias
SOLUTION:
add EnableVIewState="true" to textbox & repeater.
Dont call call dataBind() before you get the values.
Thanks!
You need to set EnableViewState to 'true' for linkbuttons to work properly in a repeater

Databinding a List to a usercontrol in an Item Template in codebehind

I have a DataList like below:
<asp:DataList runat="server" ID="myDataList">
<ItemTemplate>
<uc:MyControl ID="id1" runat="server" PublicProperty='<%# Container.DataItem %>' />
</ItemTemplate>
</asp:DataList>
The Item Template is simply a registered usercontrol, MyControl. The DataSource for the DataList is a List<List<T>> and MyControl's PublicProperty is passed List<T> which it then peforms its own databinding on. This works fine, but I have a general aversion to databinding in the aspx/c page. What is the most efficent way to set the PublicProperty value in the code behind?
If in line data binding syntax is not good enough for you - you can always hook into the ItemDatabound event of the DataList.
<asp:DataList runat="server" ID="myDataList"
OnItemDataBound="DataList_ItemDataBound">
<ItemTemplate>
<uc:MyControl ID="id1" runat="server" />
</ItemTemplate>
</asp:DataList>
Then, in the code behind of your page/containing control you can add your ItemDataBound event.
protected void DataList_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item
|| e.Item.ItemType == ListItemType.AlternatingItem)
{
DataListItem item = e.Item;
//List<string> or whatever your data source really is...
List<string> dataItem = item.DataItem as List<string>;
MyControl lit = (MyControl)item.FindControl("id1");
lit.PropertyName = dataItem;
}
}
For more information on the DataList.ItemDataBound event - Read Here
If you would rather not declare your ItemDataBound delegate inline in the ASPX you could also do it in the code behind - probably in your Page Load event:
myDataList.ItemDataBound += DataList_ItemDataBound;
Hope that helps

Categories

Resources