How to access control inside parent control? (asp.net c#) - c#

I have page with listview in it. There is label and dropdownlist in listview. I would like to access the text of label from ddlTags_Init() method.
Code:
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1"
DataKeyNames="id_Image" onitemdatabound="ListView1_ItemDataBound">
<ItemTemplate>
<asp:Label ID="TagsLabel" runat="server" Text='<%# Eval("Tags") %>' />
<asp:DropDownList ID="ddlTags" runat="server" OnInit="ddlTags_Init" >
</asp:DropDownList>
</ItemTemplate>
</asp:ListView>
Code behind:
protected void ddlTags_Init(object sender, EventArgs e)
{
DropDownList ddlTags = (DropDownList)sender;
Label lblTag = (Label)ddlTags.Parent.FindControl("TagsLabel");
string text=lblTag.Text;
}
At the moment i am stuck with
Label lblTag = (Label)ddlTags.Parent.FindControl("TagsLabel");
Anyone knows what am i missing?
Thanks, Jim

Assuming that there are more than 1 elements in the listview datasource, why don't you put your code in the ItemDataBound handler? I think that it should work.

Init is too early to get the bind value of Label. In other words, label value hasn't been bind yet.
Instead you might want to consider using ItemDataBound method.
<asp:ListView ID="ListView1" runat="server"
OnItemDataBound="ListView1_ItemDataBound" ...>
....
</asp:ListView>
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
var ddlTags = e.Item.FindControl("ddlTags") as DropDownList;
var tagsLabel = e.Item.FindControl("TagsLabel") as Label;
}
}

Related

How to get duplicate ID ASP elements from codebehind C#

My code:
<asp:DataList ID="datalist" runat="server" >
<ItemTemplate>
<asp:Textbox ID="Values" runat="server" type="text" />
</ItemTemplate>
</asp:DataList>
<asp:Button ID="Button1" runat="server" Text="SEND" OnClick="send" />
How could I get each Values ID elements of the DataList from code behind in C# ?
You loop all the items in the DataList and use FindControl to locate the TextBox.
protected void send(object sender, EventArgs e)
{
//loop all the items in the datalist
foreach (DataListItem item in datalist.Items)
{
//find the textbox with findcontrol
TextBox tb = item.FindControl("Values") as TextBox;
//do something with the textbox content
string value = tb.Text;
}
}

ItemList FindControl working for 1 item

I have a LinkButton within my ItemTemplate of my ListView:
<asp:ListView ID="lvNotification" runat="server">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lbReject" OnClick="Reject_Click" CommandArgument='<%# Eval("offerID") %>' Text="Reject" />
</ItemTemplate>
</asp:ListView>
Then in my code I have:
protected void Reject_Click(object sender, EventArgs e)
{
var lbreject = lvNotification.Items[0].FindControl("lbReject") as LinkButton;
string c = lbreject.CommandArgument;
}
The ListView retrieves 4 rows correctly, and I have placed the Eval("offerID") and it shows an offerID for each item in the list, however when I place it as the CommandArgument in the LinkButton and debug it, it shows the value 1 on ever item in the ListView, I am trying to place the offerID in each LinkButton CommandArgument and be able to access it, but I cannnot do so.
What am I doing wrong?
That is because you are always checking the first item (0 index). You can get the LinkButton from the sender and check its command argument.
protected void Reject_Click(object sender, EventArgs e)
{
var lbreject = (LinkButton)sender;
string c = lbreject.CommandArgument;
}
Here actually OnItemCommand event of ListView can be handy. This way you can control all events from the single point:
<asp:ListView ID="lvNotification" OnItemCommand="lvNotification_OnItemCommand" runat="server">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lbReject" CommandName="Reject_Click" CommandArgument='<%# Eval("offerID") %>' Text="Reject" />
</ItemTemplate>
</asp:ListView>
protected void lvNotification_OnItemCommand(object sender, ListViewCommandEventArgs e)
{
if (String.Equals(e.CommandName, "Reject_Click"))//or any other event
{
LinkButton lbreject = (LinkButton) sender;
string c = lbreject.CommandArgument;
}
}

ASP.NET Button OnClick within Repeater and LoginView

I'm developing a ASP.NET website with a C# backend. I'm having a problem with how to set an onclick event for buttons that are nested inside of both a loginview and a repeater. The code works fine for displaying all of the other data (anonymous view displays only an error message) but right now the buttons just redirect to the same page and remove the repeater and all contents, whereas they're supposed to run a specific delete function. The repeater, as it is right now, uses an alternatingitem template. If I remove the buttons from the nested controls, they work. I've tried this with buttons, linkbuttons, and imagebuttons. I'd rather use the latter, if possible. Is it possible to assign an Onclick to these buttons if they're nested like this? If not, what approach should I use?
<asp:LoginView ID="LoginLinksView" runat="server" EnableViewState="false">
<AnonymousTemplate>
<asp:Label ID="errorlabel" runat="server"></asp:Label>
</AnonymousTemplate>
<LoggedInTemplate>
<asp:Repeater id="Repeater" runat="server" >
<HeaderTemplate>
<table cellspacing="0" cellpadding="0">
<thead></thead>
</HeaderTemplate>
<ItemTemplate>
<tr class="Repeaterrow">
<!--Additional code here-->
<asp:ImageButton ID="delbutton" runat="server" ImageUrl=
"~/Images/delete.png" Onclick="DeleteOnClick"/>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr class="Repeaterrow">
<!--Additional code here-->
<asp:ImageButton ID="delbutton" runat="server" ImageUrl=
"~/Images/delete.png" Onclick="DeleteOnClick"/>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</LoggedInTemplate>
</asp:LoginView>
Here are the problems with your approach
1- The button issues postback as it should. But you need to put some CommandArgument with to identify "key" or which row you are processing it for.
2- Re Bind your Repeater with source. Below is the sample code for you.
protected void Page_Load(object sender, EventArgs e)
{
BindRepeater();
}
private void BindRepeater()
{
List<int> items = new List<int>();
for (int i = 0; i < 10; i++)
{
items.Add(i);
}
Repeater.DataSource = items;
Repeater.DataBind();
}
protected void DeleteOnClick(object sender, EventArgs e)
{
ImageButton delbutton = (sender as ImageButton);
//1- call your method with passing in delbutton.CommandArgument - it will give you key/ whatever you like
//2- Rebind the Repeater here and that will bind controls again...
BindRepeater();
}
protected void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
ImageButton delbutton = (sender as RepeaterItem).FindControl("delbutton") as ImageButton;
if (delbutton != null)
{
delbutton.CommandArgument = (sender as RepeaterItem).ItemIndex.ToString();
}
}
and ASPX Repeater definition would change to
Thanks,
Riz

How to add web user controls to repeater

Scenario:
UsrControl: custom user control, which contains a textbox and a button, rederend horizontally (in one line).
UsrControlContainer: custom user control, which should be able to display multiple UsrControl objects (each object in seperate line, so the Seperator template will probably be <br />. This control also contains a button, which adds new UsrControl to the collection.
My code:
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click"/>
<asp:Repeater ID="rptExample" runat="server">
<ItemTemplate>
</ItemTemplate>
<SeparatorTemplate><br /></SeparatorTemplate>
</asp:Repeater>
And:
protected void Button1_Click(object sender, EventArgs e)
{
rptExample.DataSource = new List<UsrControl> {new UsrControl(), new UsrControl()};
rptExample.DataBind();
}
Simple question - what should I put in ItemTemplate to make this work?
Edit - I also want to pass some parameters to UsrControl before rendering it.
<asp:Repeater ID="rptExample" runat="server">
<ItemTemplate>
<uc:UsrControl runat="server" />
</ItemTemplate>
<SeparatorTemplate><br /></SeparatorTemplate>
</asp:Repeater>
protected void Button1_Click(object sender, EventArgs e)
{
rptExample.DataSource = Enumerable.Range(0, 2);
rptExample.DataBind();
}
Following your question in answer. You can catch every binding object in ItemDataBound event. So for example, as i used, set whole object as user control property.
protected void PersonesRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
PersonLine line = (PersonLine)e.Item.FindControl("Person1");
line.Person = e.Item.DataItem as Osoba;
}
}
Ofcourse, you have to add the event handler to your repeater:
<asp:Repeater runat="server" ID="PersonesRepeater" OnItemDataBound="PersonesRepeater_ItemDataBound"><ItemTemplate>
<my:Person ID="Person1" runat="server" />
</ItemTemplate>
</asp:Repeater>

How to pass in runtime a index of row for ListView?

I have an question, On my page i have a ListView control, I also have a button that has CommandArgument. I know i can read and find a control in the ListView as :
ListView.Items[i].FindControl("controlname");
and my button in ListView is like that
asp:Button ID="WisdomButton" runat="server" CommandName="UpdateWisdom" CommandArgument='<%# need my index for Item[i] to post here %>'
OnCommand="UpdateWisdom" Text="Update" />
I want to add index value in runtime to the CommantParameter, so when i go to the Function onCommand i will know exactly from what row[i] i Need to get my controls from ListView.
So my question is, how do i dinamicly add index of ListView.Rows[i] into CommmandArgument for the Button in runtime ?
Thanks in advance.
Check out the API
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listviewcommandeventargs.aspx
The ListViewCommandEventArgs item has an index, IE it is already available in the argument
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
int i = dataItem.DisplayIndex;
But from here you will have access to those controls
e.Item.FindConrol("controlName");
If you are calling the method a different way you could aways assign the index through an ItemDataBound Event
void MyListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
((Button)e.Item.FindControl("WisdomButton")).CommandArgument = ((ListViewDataItem)e.Item).DisplayIndex;
}
OR try something like this for giggles
<asp:Button runat="server" CommandArgument='<%# DisplayIndex %>'/>
// OR
<asp:Button runat="server" CommandArgument='<%# NextIndex() %>'/>
It might help to know a little bit more about what your are trying to do. If your end goal is to get any of the properties from your bound object you can just cast the dataItem from the ListViewCommandEventArgs of the ItemCommand event. You don't need to retain the index or call FindControl at all.
Here is an example of how to get the bound Customer object.
ListView
<asp:ListView runat="server" id="Customers" ItemCommand="Customers_ItemCommand">
<LayoutTemplate>
<ul>
<asp:placeholder runat="server" id="itemPlaceholder" />
</ul>
</LayoutTemplate>
<ItemTemplate>
<li>
<asp:Button runat="server" id="Select" CommandName="Select" />
<%# Eval("Name")%>
</li>
</ItemTemplate>
</asp:ListView>
CodeBehind
public void Page_Load()
{
if (!Page.IsPostBack)
{
this.Customers.DataSource = GetCustomers();
this.Customers.DataBind();
}
}
public void Customers_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
if (e.Item.ItemType != ListViewItemType.DataItem)
return;
var customer = ((ListViewDataItem)e.Item).DataItem as Customer;
if (customer != null)
{
// Now work directly with the customer object.
Response.Redirect("viewCustomer.aspx?id=" + customer.Id);
}
}
}
Edit: In addtion when you cast the item to a ListViewDataItem, then you also expose a ((ListViewDataItem)e.Item).DataItemIndex property that might help you.

Categories