angelalaw
asked on
Page_Load Manual bind Dropdownlist in gridview footer
Hi all,
I am putting a dropdown in the footer of my grid with some creative <div>ing.
I need populate it and it is based off other control selections.
so for PAGE_LOAD
How can I manually populate a dropdrown in a gridview's footer?
I have tried it and it appears that the gridview has yet loaded when my code gets called. there for it thinks the dropdown is null...
ASPX
------------
<FooterTemplate>
<div style="text-align:center;" ><input id="chkAllBottom" onclick="return SelectAllCheckboxes(this); " runat="server" type="checkbox" /></div>
<div style="position:relative;t op:-23px;l eft:600px; ">
<table cellpadding = "0" cellspacing="0" border="0" width="350" class="selectedAction" style="position:absolute">
<tr>
<td valign="middle" width="150" align="left"><strong>With Selected</strong></td>
<td valign="middle" width="150" align="right">
<asp:DropDownList ID="_action" runat="server" EnableViewState="true" />
</td>
<td valign="middle" width="50" align="right">
<asp:Button ID="btnGo" runat="server" Text="Go" />
</td>
</tr>
</table>
</div>
-----------
CodeBehind being called from page_load
-------------
protected void setAction()
{
StringBuilder sSQL = new StringBuilder();
sSQL.Append(" SELECT id=1, template_name='-- Choose Action --' UNION ");
registration_tool RegTool = new registration_tool();
DropDownList _action = ((DropDownList) _mygridview.FindControl("_ action")); <--- returns NULL
RegTool.setStatus(sSQL,_ac tion,_netw ork.Select edValue.To String()); <--fails databinding cause control is NULL
_action.SelectedIndex = 0;
}
I am putting a dropdown in the footer of my grid with some creative <div>ing.
I need populate it and it is based off other control selections.
so for PAGE_LOAD
How can I manually populate a dropdrown in a gridview's footer?
I have tried it and it appears that the gridview has yet loaded when my code gets called. there for it thinks the dropdown is null...
ASPX
------------
<FooterTemplate>
<div style="text-align:center;"
<div style="position:relative;t
<table cellpadding = "0" cellspacing="0" border="0" width="350" class="selectedAction" style="position:absolute">
<tr>
<td valign="middle" width="150" align="left"><strong>With Selected</strong></td>
<td valign="middle" width="150" align="right">
<asp:DropDownList ID="_action" runat="server" EnableViewState="true" />
</td>
<td valign="middle" width="50" align="right">
<asp:Button ID="btnGo" runat="server" Text="Go" />
</td>
</tr>
</table>
</div>
-----------
CodeBehind being called from page_load
-------------
protected void setAction()
{
StringBuilder sSQL = new StringBuilder();
sSQL.Append(" SELECT id=1, template_name='-- Choose Action --' UNION ");
registration_tool RegTool = new registration_tool();
DropDownList _action = ((DropDownList) _mygridview.FindControl("_
RegTool.setStatus(sSQL,_ac
_action.SelectedIndex = 0;
}
It returns null as FindControl only works on the container it has been called on not it's child control, you need a recursive function to call findcontrol on each sub container and all children.
Using you example if you want to find the "_action" in "_mygridview": -
DropDownList _action = ((DropDownList) FindControl(_mygridview,"_ action"));
You would obviosuly want to deal with null returns from the FindControl function but the above is a very simplistic solution.
/// <summary>
/// Returns a control if one by that name exists in the hierarchy of the controls collection of the start control
/// </summary>
/// <param name="start">Control to begin search from</param>
/// <param name="id">The ID of the control</param>
/// <returns></returns>
public static Control FindControl(Control start, string id)
{
Control foundControl;
if (start != null)
{
foundControl = start.FindControl(id);
if (foundControl != null)
return foundControl;
foreach (Control c in start.Controls)
{
foundControl = FindControl(c, id);
if (foundControl != null)
return foundControl;
}
}
return null;
}
DropDownList _action = ((DropDownList) FindControl(_mygridview,"_
You would obviosuly want to deal with null returns from the FindControl function but the above is a very simplistic solution.
/// <summary>
/// Returns a control if one by that name exists in the hierarchy of the controls collection of the start control
/// </summary>
/// <param name="start">Control to begin search from</param>
/// <param name="id">The ID of the control</param>
/// <returns></returns>
public static Control FindControl(Control start, string id)
{
Control foundControl;
if (start != null)
{
foundControl = start.FindControl(id);
if (foundControl != null)
return foundControl;
foreach (Control c in start.Controls)
{
foundControl = FindControl(c, id);
if (foundControl != null)
return foundControl;
}
}
return null;
}
ASKER
Hi Mcexp,
Thanks so much for responding. I tried what you put... And I am still getting a null returned for the control....
I have the feeling it is a rendering issue. I also feel you are right I wasn't referencing it right... but I seem to remember reading long ago problems like this when people where trying to manually set things when a grid loaded.
That they had to do it onrowdatabound function... But I am not sure if that applies to me as this is a footer part and not a row part.
Again I truly appreciate any help provided :)
Angela
Thanks so much for responding. I tried what you put... And I am still getting a null returned for the control....
I have the feeling it is a rendering issue. I also feel you are right I wasn't referencing it right... but I seem to remember reading long ago problems like this when people where trying to manually set things when a grid loaded.
That they had to do it onrowdatabound function... But I am not sure if that applies to me as this is a footer part and not a row part.
Again I truly appreciate any help provided :)
Angela
I have found this example on the internet (http://www.gridviewguy.com/ArticleDetails.aspx?articleID=120)?
Would this help?
protected void Button1_Click1(object sender, EventArgs e)
{
GridViewRow row = GridView1.FooterRow;
string firstName = ((TextBox)row.FindControl( "TextBox1" )).Text;
string lastName = ((TextBox)row.FindControl( "TextBox2" )).Text;
Label1.Text = "Data Entered in the Footer Row:" + lastName + " " + firstName;
}
Would this help?
protected void Button1_Click1(object sender, EventArgs e)
{
GridViewRow row = GridView1.FooterRow;
string firstName = ((TextBox)row.FindControl(
string lastName = ((TextBox)row.FindControl(
Label1.Text = "Data Entered in the Footer Row:" + lastName + " " + firstName;
}
ASKER
For those looking for the correct answer, I finally figured it out....
After the grid is bound do something like (syntax may not be totally correct...
Dim footerRow As GridViewRow = CustomersGridView.FooterRo w
Dim DDL as DropDownList = footerRow.FindControl("myd ropdown")
then you can manipulate the control....
I wish there was a way to mark this post as the solution (without points) becuase this is the correct solution...
Thanks for those who tried to help,
Angela
After the grid is bound do something like (syntax may not be totally correct...
Dim footerRow As GridViewRow = CustomersGridView.FooterRo
Dim DDL as DropDownList = footerRow.FindControl("myd
then you can manipulate the control....
I wish there was a way to mark this post as the solution (without points) becuase this is the correct solution...
Thanks for those who tried to help,
Angela
Wasn't my example close enough?
I showed how to Get the Footor row
and then how to access controls
your solution is in VB yet you posted in a C# group hence why I posted in C#
If you wanted the exact code I could have given it last week.
I showed how to Get the Footor row
and then how to access controls
your solution is in VB yet you posted in a C# group hence why I posted in C#
If you wanted the exact code I could have given it last week.
ASKER
Hi McExp,
While I appreciate your response...Unfortunately you code yielded a NULL result which was unable to be used. if you want to be a stickler about code here I will post it in c#...
gvEmployeeList.DataBind(); <---must be done first....
GridViewRow footer = gvEmployeeList.FooterRow() ;
DropDownList ddl = DropDownList (footer.FindControl("mydro pdown"));
What you had given was a recursive function which produced a null..
The above inofrmation is what I used to do the problem and my original way of thinking with FindControl on a row (not recursive) was correct. I just didn't know how to get the footer row...
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.footerrow.aspx
this link that I found helped me...
While I appreciate your response...Unfortunately you code yielded a NULL result which was unable to be used. if you want to be a stickler about code here I will post it in c#...
gvEmployeeList.DataBind();
GridViewRow footer = gvEmployeeList.FooterRow()
DropDownList ddl = DropDownList (footer.FindControl("mydro
What you had given was a recursive function which produced a null..
The above inofrmation is what I used to do the problem and my original way of thinking with FindControl on a row (not recursive) was correct. I just didn't know how to get the footer row...
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.footerrow.aspx
this link that I found helped me...
I acknowledge that the Find Control Recursion was targeted at the wrong container which was why I posted a second solution (with URL for full explantion) which I would have thought you would agree looks very similar to your final solution, albet accessing Text Boxes rather than a Drop down list.
The Url I posted was to an article titled "Accessing GridView Header and Footer" did that not give you the information you needed
The Url I posted was to an article titled "Accessing GridView Header and Footer" did that not give you the information you needed
ASKER
Hi McExp,
You are right you did show that... Totally didn't realize that... anyway, when I looked at your solution briefly... I noticed it was in a callback event....and knew it wouldn't work...What I didn't state clearly enough in my first post do it *before* or *during* the page rendering so I didn't really pay attention to the rest of your second post...
Just like you didn't pay attention to my second post when I said I think it is a rendering problem...
What the main problem was two fold...One it was using the footerrow with the findcontrol... Second was even though I was accessing the footer row and not any data rows... the datarows needed to be binded before the grid would allow acccess to the footer row...
So I will give you part of the solution... Sorry for the confusion and the stubbornness... jsut a bad week on my part...
However, the proprosed solution (I don't know if you did that or they did) is not the correct one. And since you got half of the problem...I will award partial points to you...for the second post...
BTW that link you gave never did work for me it gives a Yellow ASP.NET error screen...
I truly appreciate the time and help you offered,
Angela
You are right you did show that... Totally didn't realize that... anyway, when I looked at your solution briefly... I noticed it was in a callback event....and knew it wouldn't work...What I didn't state clearly enough in my first post do it *before* or *during* the page rendering so I didn't really pay attention to the rest of your second post...
Just like you didn't pay attention to my second post when I said I think it is a rendering problem...
What the main problem was two fold...One it was using the footerrow with the findcontrol... Second was even though I was accessing the footer row and not any data rows... the datarows needed to be binded before the grid would allow acccess to the footer row...
So I will give you part of the solution... Sorry for the confusion and the stubbornness... jsut a bad week on my part...
However, the proprosed solution (I don't know if you did that or they did) is not the correct one. And since you got half of the problem...I will award partial points to you...for the second post...
BTW that link you gave never did work for me it gives a Yellow ASP.NET error screen...
I truly appreciate the time and help you offered,
Angela
ASKER
MCExp,
Do me a favor, since I can't award partial points to could you post the following:
gvEmployeeList.DataBind(); <---must be done first....
GridViewRow footer = gvEmployeeList.FooterRow() ;
DropDownList ddl = DropDownList (footer.FindControl("mydro pdown"));
and I will give you full points on that post as the accepted solution...
I can't do it right now because the solution you offered didn't take into account the rendering problem I mentioned in my second post...
and if the grid is NOT databinded BEFORE referencing the footerrow your solution won't work....
I like to make the knowledgebase as accurately as possible...
Angela law
Do me a favor, since I can't award partial points to could you post the following:
gvEmployeeList.DataBind();
GridViewRow footer = gvEmployeeList.FooterRow()
DropDownList ddl = DropDownList (footer.FindControl("mydro
and I will give you full points on that post as the accepted solution...
I can't do it right now because the solution you offered didn't take into account the rendering problem I mentioned in my second post...
and if the grid is NOT databinded BEFORE referencing the footerrow your solution won't work....
I like to make the knowledgebase as accurately as possible...
Angela law
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.