How to access Repeater FooterTemplate TextBox? - asp.net

What I am trying to do is to access the value from the TextBox1 to display back on the screen.
I tried to access it from the Page_Load() and the OnItemDataBound and they both failed.
It seem like the code is able to access the control but it return nothing back.
protected void Page_Load(object sender, EventArgs e)
{
Literal Literal1 = (Literal)Repeater1.Controls[Repeater1.Controls.Count - 1].FindControl("Literal1");
Response.Write(Literal1.Text);
//this techique is not working for the line below
TextBox TextBox1 = (TextBox)Repeater1.Controls[Repeater1.Controls.Count - 1].FindControl("TextBox1");
Response.Write(TextBox1.Text);
}
public void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
//this techique is not working
if (e.Item.ItemType == ListItemType.Footer)
{
TextBox TextBox1 = (TextBox)e.Item.FindControl("TextBox1");
Response.Write(TextBox1.Text);
}
}

I'm not sure what you mean by "they both failed" but if the Text property of the Textbox is empty i might be because you are rebinding your repeater on each post back. Try wrapping your repeater .DataBind() with a !IsPostBack condition.

I have to use "Literal" as an alternate solution by passing the html Textbox problem. I do not like this solution but i guess i have to use it.

Related

how I find a control in datalist

I have taken a datalist in my asp.net project which contains some picture and picture name. Now I want to show each picture in another page after. It means if any user will click on the picture then picture will open with it's details in another page.So I want to know that how I find a control in datalist.
In following picture of datalist I want to click on picture name like Jai Ho and It will open in another page.
You can use the OnClientClick property of Link Button.
In <asp:LinkButton> tag add following code
OnClientClick="javascript:window.open('your_url');"
try this
protected void up-movie-name_Click(object sender, EventArgs e)
{
Label4.Text = (Datalist1.SelectedItem.FindControl("Label1").ToString());
}
or
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
LinkButton linkButton = e.Item.FindControl("LinkButton1") as LinkButton;
string commandText = linkButton.Text;
}
}

asp.net listview in ajax panel not refreshing after databind

I am having a problem updating a listview's databinding when inside a update panel. The listview is to do a databind and return some user names.
I have tested the databind by triggering it from an asp button and it works fine like so.
protected void getFacebookFriends(object sender, EventArgs e)
{
List<testResult> getFriends = (from i in lqDataContext.test(base.UserId) select i).ToList();
lvFacebookFriends.DataSource = getFriends;
lvFacebookFriends.DataBind();
}
when I try do this via ajax using
<telerik:RadAjaxPanel ID="updateFriends" runat="server" OnAjaxRequest="updateFriends_AjaxRequest">
function invokeAjaxRequest() {
$find("<%= updateFriends.ClientID%>").ajaxRequestWithTarget("<%= updateFriends.UniqueID %>", 97);
}
protected void updateFriends_AjaxRequest(object sender, AjaxRequestEventArgs e)
{
List getFriends = (from i in lqDataContext.test(base.UserId) select i).ToList();
lvFacebookFriends.DataSource = getFriends;
lvFacebookFriends.DataBind();
updateFriends.EnableAJAX = false;
}
I can see in debug that I get through updateFriends_AjaxRequest without error but the ui has not change.
Thanks for helping me understand this.
Mark
Just wrap it in a regular update panel. The postback will be caught and the listview will re-render with the new information asynchronously.

not able to find usercontrol in edittemplate of datalist

i have a webusercontrol in the edittemplate of the datalist. in the code behind, in the itemCommand, when i try to find it using findcontrol, i get null object.
what is it that i am doing wrong?
WebUserControl cntrl = (WebUserControl)e.Item.FindControl("myControl");
or i also tried the below, in the EditCommand event, because i have kept the usercontrol inside the EditTemplate of the DataList:
WebUserControl cntrl = (WebUserControl)DataList1.FindControl("myControl");
I think you're probably doing something like this :
protected void gridView_rowDataBound(Object sender, GridViewRowEventArgs e)
{
WebUserControl cntrl = (WebUserControl)e.Item.FindControl("myControl");
}
Keep in mind that you'd be looking at every single row - including the header and footer rows.
I think you need this :
protected void gridView_rowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow){
WebUserControl cntrl = (WebUserControl)e.Item.FindControl("myControl");
}
}
Actually never mind, i moved the control to the headertemplate and looking at its controls collection, i am able to find the control using FindControl. Not sure why its not finding if i place it in the edititemtemplate. but thanks guys appreciate your help.

Get FormView From Its InsertItemTemplate Child Button

I have a FormView on my WebPage and I am trying to get the FormView when my Submit button is pressed inside the FormView's InsertItemTemplate. How can I do that. My sender of the event is the button but not the FormView. Is there any property for it?
You should be able to access FV directly via it's ID in the ButtonClick.
protected void Button1_Click(object sender, EventArgs e)
{
string id = FormView1.ID;
//Here is an explicit way to do get FV
//FormView fv = (FormView)((Button)sender).NamingContainer;
}

How to use the FindControl function to find a dynamically generated control?

I have a PlaceHolder control inside of a ListView that I am using to render controls from my code behind. The code below adds the controls:
TextBox tb = new TextBox();
tb.Text = quest.Value;
tb.ID = quest.ShortName.Replace(" ", "");
((PlaceHolder)e.Item.FindControl("ph_QuestionInput")).Controls.Add(tb);
I am using the following code to retrieve the values that have been entered into the TextBox:
foreach (ListViewDataItem di in lv_Questions.Items)
{
int QuestionId = Convert.ToInt32(((HiddenField)di.FindControl("hf_QuestionId")).Value);
Question quest = dc.Questions.Single(q => q.QuestionId == QuestionId);
TextBox tb = ((TextBox)di.FindControl(quest.ShortName.Replace(" ","")));
//tb is always null!
}
But it never finds the control. I've looked at the source code for the page and the control i want has the id:
ctl00_cphContentMiddle_lv_Questions_ctrl0_Numberofacres
For some reason when I look at the controls in the ListViewDataItem it has the ClientID:
ctl00_cphContentMiddle_lv_Questions_ctrl0_ctl00
Why would it be changing Numberofacres to ctl00? Is there any way to work around this?
UPDATE:
Just to clarify, I am databinding my ListView in the Page_Init event. I then create the controls in the ItemBound event for my ListView. But based on what #Womp and MSDN are saying the controls won't actually be created until after the Load event (which is after the Page_Init event) and therefore are not in ViewState? Does this sound correct?
If so am I just SOL when it comes to retrieving the values in my dynamic controls from my OnClick event?
UPDATE 2:
So i changed the code i had in my Page_Init event from:
protected void Page_Init(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//databind lv_Questions
}
}
to:
protected void Page_Init(object sender, EventArgs e)
{
//databind lv_Questions
}
And it fixed my problem. Still a little confused as to why I want to databind regardless of whether it's a postback or not but the issue is resolved.
It looks like you're adding your textbox to a Placeholder control... but then you're searching a ListViewDataItem container for it later.
Seems to me that you need to search for the Placeholder first, and then search it for the textbox.

Resources