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;
}
}
Related
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:TemplateField HeaderText="Team Leader">
<ItemTemplate>
<asp:Label ID="gvuser_teamleader" runat="server" Text='<%# Bind("TeamLeaderID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtuserteamleader" runat="server" Width="100px" Text='<%# Eval("TeamLeaderID") %>' CssClass="textboxstyle roundedcorner aligncenter gradientskyblue"></asp:TextBox>
<asp:ListBox ID="listboxuserteamleader" runat="server" Width="110px" AutoPostBack="true" OnSelectedIndexChanged="listboxuserteamleader_SelectedIndexChanged" CssClass="textboxstyle roundedcorner aligncenter gradientskyblue"></asp:ListBox>
<asp:DropDownExtender ID="DropDownExtender3" runat="server" TargetControlID="txtuserteamleader" DropDownControlID="listboxuserteamleader"></asp:DropDownExtender>
</EditItemTemplate>
</asp:TemplateField>
When I am trying to fire the selected listbox index changed event and trying to bind the listbox selected value to textbox am getting this error. Also both controls are inside the gridview edit item template field.
My code:
protected void listboxuserteamleader_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (GridViewRow gvr in gvusers.Rows)
{
TextBox txtuserteamleader = (TextBox)gvusers.FindControl("txtuserteamleader");
ListBox listboxuserteamleader = (ListBox)gvusers.FindControl("listboxuserteamleader");
txtuserteamleader.Text = listboxuserteamleader.SelectedValue.ToString();
}
}
just try the below code:
protected void listboxuserteamleader_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (GridViewRow gvr in gvusers.Rows)
{
TextBox txtuserteamleader = (TextBox)gvr.FindControl("txtuserteamleader");
ListBox listboxuserteamleader = (ListBox)gvr.FindControl("listboxuserteamleader");
if(txtuserteamleader !=null && listboxuserteamleader !=null)
{
txtuserteamleader.Text = listboxuserteamleader.SelectedValue.ToString();
}
}
}
Actually what was you problem : you have created instance of gridview "gvusers" as gvr for each row... so in foreach you must have to use that instance not the "gvusers" ... here you were making mistake...
That's all
try this:
protected void listboxuserteamleader_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (GridViewRow gvr in gvusers.Rows)
{
TextBox txtuserteamleader = (TextBox)gvr.FindControl("txtuserteamleader");
ListBox listboxuserteamleader = (ListBox)gvr.FindControl("listboxuserteamleader");
txtuserteamleader.Text = listboxuserteamleader.SelectedValue.ToString();
}
}
When you iterate through the rows of the GridView, you have to search for the control inside that row, so the error you made is that you were searching it in the entire GridView, where each row would have this control. So you need to search it in a row.
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;
}
}
I have created a DataList in asp.net -
<asp:DataList runat="server" ID="pTextBox">
<ItemTemplate>
<asp:CheckBox ID="CheckBoxPN" runat="server" Checked='false' />
<asp:TextBox ID="profileTextBox" runat="server" Text='<%# Container.DataItem.ToString() %>'></asp:TextBox>
</ItemTemplate>
</asp:DataList>
This creates checkBoxes and textBoxes based on the string values passed through from a webService.
How can I get the profileTextBox Text string value when a user clicks CheckBoxPN and populate another textBox outwith the DataList on the page with the string value??
You can use the CheckedChanged event of the CheckBox and cast it's NamingContainer to DataListItem, the you just have to use FindControl to find a different server control:
protected void CheckBoxPN_CheckedChanged(Object sender, EventArgs e)
{
CheckBox chk = (CheckBox) sender;
DataListItem item = (DataListItem) chk.NamingContainer;
TextBox txt = (TextBox) item.FindControl("profileTextBox");
this.OtherTextBoxOnPage.Text = txt.Text; // here we are
}
By the way, this approach works with any web-databound control(Repeater, GridView, etc.)
Greetings!
I have a DropDownList within a FormView which are bound to XmlDataSources:
<asp:FormView ID="MyFormView" runat="server" DataSourceID="MyXmlDataSource">
<ItemTemplate>
<h1><%# XPath("SomeNode")%></h1>
<asp:Label ID="MyLabel" runat="server" AssociatedControlID="MyDdl" Text='<%# XPath("SomeOtherNode")%>' />
<asp:DropDownList ID="MyDdl"
runat="server"
DataSourceID="MyDdlDataSource"
DataTextField="name"
DataValueField="value"
AutoPostBack="true"
OnSelectedIndexChanged="MyDdl_SelectedIndexChanged">
</asp:DropDownList>
</ItemTemplate>
</asp:FormView>
<asp:XmlDataSource ID="MyXmlDataSource" runat="server" XPath="Root/MainSection" />
<asp:XmlDataSource ID="MyDdlDataSource" runat="server" XPath="Root/MainSection/Areas/*" />
In the page's codebehind, I have the following OnLoad() method as well as the method for when the select index of the dropdownlist changes:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (!IsPostBack)
{
string xml = GetMyXml(0); // default value
MyXmlDataSource.Data = xml;
MyDdlDataSource.Data = xml;
}
}
protected void MyDdl_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList l_MyDdl = FindControl("MyDdl") as DropDownList;
int myVal;
if (l_MyDdl != null)
if (!Int32.TryParse(l_MyDdl.SelectedItem.Value, out myVal))
myVal = 0;
string xml = GetMyXml(myVal);
MyXmlDataSource.Data = xml;
MyDdlDataSource.Data = xml;
}
When a different value is selected from the dropdown list and SelectedIndexChanged is invoked, I am unable to get the value of the dropdown list (FindControl always returns null) in order to use it to re-bind the datasources. How can I get this value?
Because your dropdownlist is contained within another control it may be that you need a recursive findcontrol.
http://weblogs.asp.net/palermo4/archive/2007/04/13/recursive-findcontrol-t.aspx