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.
Related
I have a telerik radgrid in my .aspx page name rgLowRise, and I have an ObjectDataSource as the DataSource set like rgLowRise.DataSourceID = odsLowRise. This works fine, but I want it not to show any records at first load, how can I do that? And I have to use DataSourceID and not DataSource due to some reasons.
Thanks
You can remove the DataSourceID from the markup or your code behind (Page_Load) and set at a later stage, e.g. Button_Click or some other event
protected void Button_Click(object sender, EventArgs e)
{
rgLowRise.DataSourceID = odsLowRise; //until you click this button, the grid would display nothing
}
I am getting Application name from querystring.
I have GridView control that I can use to insert some Tasks. I have to insert Tasks based on the Application name I am getting in the querystring. I am inserting Record in the GridView footer. Since, application name is coming from Querystring, I want to show it as one item in the GridView footer. How can I do this while Page_Loading time?
Thanks.
You will need to bind to the RowDataBound event and then check the event to see if it is the footer like so:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[0].Text = Request["ApplicationName"];
}
}
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.
I have a listview and DataPager control. Now when I click a page number on the DataPager, the
ListView1_PagePropertiesChanging(object
sender,
PagePropertiesChangingEventArgs e)
event is fired. Now, I would like to identify which page number of the DataPager was clicked. For example, if Page 3 was clicked, then I would like to get the value 3. How can I do this?
Thanks in advance.
you can try a code like this.
protected void ListView1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
int pageindex = e.StartRowIndex / e.MaximumRows;
}
The PagePropertiesChangingEventArgs as members that have some of the information you might need
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.