aracen74
asked on
Multiple Controls in GridView Template Field and Perform Update
First Thank You for Reading
I have an image and a dropdownlist in a GridView Template Field.
If the user clicks the image, the dropdownlist displays
When user selects dropdownlist value the new value is updated to the database
If the user clicks the image and the dropdownlist is visible then , dropdownlist visible is set to false
I can get as far as getting the index of the image, how do I build a reference to the dropdownlist in the same row.
// start aspx page
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:ImageButton
ID="imgRenumber"
runat="server"
CausesValidation="false"
CommandName="Show"
ImageUrl="~/Images/EETMove .gif"
AlternateText="Manual Renumber"
CommandArgument='<%# Container.DataItemIndex %>' />
<asp:DropDownList ID="ddlRenumber" runat="server" AutoPostBack="True" Visible="false">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
// end aspx
//start aspx.cs
// This Part Works!
protected void grdMasterExhibit_RowCreate d(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow )
{
ImageButton button0 = (ImageButton)e.Row.FindCon trol("imgR enumber");
button0.CommandArgument = e.Row.RowIndex.ToString();
}
}
// This Part Blows Up
protected void grdMasterExhibit_RowComman d(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandA rgument); // This is the image
GridViewRow grid0 = (GridViewRow)e.CommandSour ce;
//The Error --
// Unable to cast object of type 'System.Web.UI.WebControls .ImageButt on'
// to type 'System.Web.UI.WebControls .GridViewR ow'.
switch (e.CommandName)
{
case "Show":
DropDownList dropDown1 = (DropDownList)e.Row.FindCo ntrol("ddl Renumber") ;
// The Row here is invalid because image is coming in not the grid view
dropDown1.Visible = true;
break;
case "Hide":
DropDownList dropDown1 = (DropDownList)e.Row.FindCo ntrol("ddl Renumber") ;
// The Row here is invalid because image is coming in not the grid view
dropDown1.Visible = false;
break;
}
}
}
//end aspx.cs
I have an image and a dropdownlist in a GridView Template Field.
If the user clicks the image, the dropdownlist displays
When user selects dropdownlist value the new value is updated to the database
If the user clicks the image and the dropdownlist is visible then , dropdownlist visible is set to false
I can get as far as getting the index of the image, how do I build a reference to the dropdownlist in the same row.
// start aspx page
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:ImageButton
ID="imgRenumber"
runat="server"
CausesValidation="false"
CommandName="Show"
ImageUrl="~/Images/EETMove
AlternateText="Manual Renumber"
CommandArgument='<%# Container.DataItemIndex %>' />
<asp:DropDownList ID="ddlRenumber" runat="server" AutoPostBack="True" Visible="false">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
// end aspx
//start aspx.cs
// This Part Works!
protected void grdMasterExhibit_RowCreate
{
if (e.Row.RowType == DataControlRowType.DataRow
{
ImageButton button0 = (ImageButton)e.Row.FindCon
button0.CommandArgument = e.Row.RowIndex.ToString();
}
}
// This Part Blows Up
protected void grdMasterExhibit_RowComman
{
int index = Convert.ToInt32(e.CommandA
GridViewRow grid0 = (GridViewRow)e.CommandSour
//The Error --
// Unable to cast object of type 'System.Web.UI.WebControls
// to type 'System.Web.UI.WebControls
switch (e.CommandName)
{
case "Show":
DropDownList dropDown1 = (DropDownList)e.Row.FindCo
// The Row here is invalid because image is coming in not the grid view
dropDown1.Visible = true;
break;
case "Hide":
DropDownList dropDown1 = (DropDownList)e.Row.FindCo
// The Row here is invalid because image is coming in not the grid view
dropDown1.Visible = false;
break;
}
}
}
//end aspx.cs
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
http://msmvps.com/blogs/SimpleMan/archive/2005/08/06/61782.aspx
case "Show":
GridViewRow gvr = (GridViewRow)(((Control)e. CommandSou rce).Namin gContainer );
DropDownList dropDown1 = (DropDownList)gvr.FindCont rol("DropD ownList1") ;
dropDown1.Visible = true;
break;
case "Show":
GridViewRow gvr = (GridViewRow)(((Control)e.
DropDownList dropDown1 = (DropDownList)gvr.FindCont
dropDown1.Visible = true;
break;
Hello aracen74,
First of all, glad you got it worked out.
Secondly, your solution is the same as mine. e.CommandSource = sender.
First of all, glad you got it worked out.
Secondly, your solution is the same as mine. e.CommandSource = sender.
ASKER
Thanks Again :-)
ASKER
Here's what I found out
case "Show":
GridViewRow gvr = (GridViewRow)(((Control)e.
DropDownList dropDown1 = (DropDownList)gvr.FindCont
dropDown1.Visible = true;
break;