ASP.NET2.0中GridView嵌套取值的问题!
在GridView1中嵌套了一个GridView4,当点击GridView4中某一行时,怎样才能取到当前GridView4在GridView1中所在的行???
<asp:GridView ID= "GridView1 " runat= "server " AutoGenerateColumns= "False " OnRowDataBound= "GridView1_RowDataBound " ShowFooter= "True " OnRowCommand= "GridView1_RowCommand " DataKeyNames= "type ">
<Columns>
<asp:BoundField DataField= "type " HeaderText= "type " />
<asp:TemplateField HeaderText= "Case ">
<ItemTemplate>
<asp:GridView ID= "GridView4 " runat= "server " AutoGenerateColumns= "False " ShowFooter= "True " OnRowDataBound= "GridView4_RowDataBound " OnRowCommand= "GridView4_RowCommand " DataKeyNames= "pub_id,type,price,advance ">
<Columns>
<asp:BoundField DataField= "pub_id " HeaderText= "Pub_id " />
<asp:BoundField DataField= "price " HeaderText= "Price " HtmlEncode= "False " NullDisplayText= "0 " />
<asp:BoundField DataField= "advance " HeaderText= "Advance " />
<asp:ButtonField CommandName= "a " Text= "按钮 " DataTextField= "type " />
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
titlesTypeTableAdapter titlesType = new titlesTypeTableAdapter();
GridView1.DataSource = titlesType.GetTitlesByType();
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string type = e.Row.Cells[0].Text;
titlesTypeTableAdapter titlesType = new titlesTypeTableAdapter();
((GridView)e.Row.Cells[1].FindControl( "GridView4 ")).DataSource = titlesType.GetPub_idByType(type);
((GridView)e.Row.Cells[1].FindControl( "GridView4 ")).DataBind();
}
}
protected void GridView4_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.CompareTo( "a ") == 0)
{
int i = 0; // i表示当前GridView4在GridView1中所在的行;
//得到所选按扭中的值;
string pub_id = (string)((GridView)GridView1.Rows[i].FindControl( "GridView4 ")).DataKeys[Convert.ToInt32(e.CommandArgument)].Value;
string type = (string)((GridView)GridView1.Rows[i].FindControl( "GridView4 ")).DataKeys[Convert.ToInt32(e.CommandArgument)].Values[1];
}
}
[解决办法]
GridView4_RowCommand
GridView a = (GridView)sender;
System.Web.UI.WebControls.GridViewRow row = (System.Web.UI.WebControls.GridViewRow)a.NamingContainer;
[解决办法]
NamingContainer 或 .Parent.Parent 去引用到都可以