I want to retrieve the selected value from a TextBox that is in a ListView ItemTemplate. Below is the code that is on my source page that displays multiple TextBoxes with different ID values.
In code behind is it possible to set a public property for TextBox1? Somthing similar to this
public string ID
{
get { return (ListView)ListGalleries.FindControl("TextBox1"); }
}
When the user clicks on the "Select Button" the Text value of TextBox1 needs to be available on the TargetPage.aspx I have a PreviousPageType declared on TargetPage.aspx, so how can I retrieve the value from the postback from the ListView?
(((sender as Button).Parent as ListViewItem).FindControl("TextBox1") as TextBox).Text;
This will get the text from the textBox that is in the same ListViewItem as the Button. You cannot use the findcontrol method on the ListView because there will be as many copies of it as there are items in your data set. I used this code to test:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
List<TestData> dataList = new List<TestData>();
for (int i = 0; i < 10; i++)
{
dataList.Add(new TestData { ID = i.ToString() });
}
ListGalleries.DataSource = dataList;
ListGalleries.DataBind();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
testLabel.Text = (((sender as Button).Parent as ListViewItem).FindControl("TextBox1") as TextBox).Text; }
Now that you have the value you can pass it on to the target page.
bdassow
Member
15 Points
32 Posts
How do you retrieve a value from a ListView to use in a postback?
Feb 18, 2013 09:02 PM|LINK
I want to retrieve the selected value from a TextBox that is in a ListView ItemTemplate. Below is the code that is on my source page that displays multiple TextBoxes with different ID values.
<asp:ListView ID="ListGalleries" runat="server" DataKeyNames="ID" ItemPlaceholderID="itemPlaceholderContainer" > <ItemTemplate> <asp:Button ID="Button2" runat="server" Text="Select Button" PostBackUrl="~/TargetPage.aspx" /> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("ID") %>'></asp:TextBox> </ItemTemplate> </asp:ListView>In code behind is it possible to set a public property for TextBox1? Somthing similar to this
public string ID { get { return (ListView)ListGalleries.FindControl("TextBox1"); } }
When the user clicks on the "Select Button" the Text value of TextBox1 needs to be available on the TargetPage.aspx
I have a PreviousPageType declared on TargetPage.aspx, so how can I retrieve the value from the postback from the ListView?
Thanks in advance for you assistance.
gazalam
Member
14 Points
2 Posts
Re: How do you retrieve a value from a ListView to use in a postback?
Feb 22, 2013 08:32 PM|LINK
(((sender as Button).Parent as ListViewItem).FindControl("TextBox1") as TextBox).Text;This will get the text from the textBox that is in the same ListViewItem as the Button. You cannot use the findcontrol method on the ListView because there will be as many copies of it as there are items in your data set. I used this code to test:
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { List<TestData> dataList = new List<TestData>(); for (int i = 0; i < 10; i++) { dataList.Add(new TestData { ID = i.ToString() }); } ListGalleries.DataSource = dataList; ListGalleries.DataBind(); } } protected void Button2_Click(object sender, EventArgs e) { testLabel.Text = (((sender as Button).Parent as ListViewItem).FindControl("TextBox1") as TextBox).Text; }Now that you have the value you can pass it on to the target page.
I hope this helps a little.
gazalam
Member
14 Points
2 Posts
Re: How do you retrieve a value from a ListView to use in a postback?
Feb 25, 2013 01:07 PM|LINK
Since the target page is within the same application you could just put the data into the Session dictionary.
HttpContext.Current.Session["ID"] = (((sender as Button).Parent as ListViewItem).FindControl("TextBox1") as TextBox).Text;