急求!如何在GridView的OnRowCommand事件 用FindControl找到模版控件?
Html:<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"OnRowCommand...
Html:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" Width="100%">
<Columns>
<asp:TemplateField HeaderText="序号" Visible="False">
<ItemTemplate>
<asp:Label ID="Lab_DId" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "CloDId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CommandName="Edit" Text="编辑" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button2" runat="server" CommandName="Delete" Text="删除" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#8080FF" />
</asp:GridView>
CS:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.ToString().Trim() == "Edit")
{
string ClDId = ((Label)(GridView1.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("Lab_DId"))).Text.Trim();
//e.CommandArgument调试数据为空
string ClDId1 = ((Label)(GridView1.SelectedRow.FindControl("Lab_DId"))).Text.Trim();
}
}
这两种都出错怎么回事啊?
大家帮帮忙啊! 展开
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" Width="100%">
<Columns>
<asp:TemplateField HeaderText="序号" Visible="False">
<ItemTemplate>
<asp:Label ID="Lab_DId" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "CloDId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CommandName="Edit" Text="编辑" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button2" runat="server" CommandName="Delete" Text="删除" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#8080FF" />
</asp:GridView>
CS:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.ToString().Trim() == "Edit")
{
string ClDId = ((Label)(GridView1.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("Lab_DId"))).Text.Trim();
//e.CommandArgument调试数据为空
string ClDId1 = ((Label)(GridView1.SelectedRow.FindControl("Lab_DId"))).Text.Trim();
}
}
这两种都出错怎么回事啊?
大家帮帮忙啊! 展开
1个回答
展开全部
RowDataBound事件里面把行的索引绑定到button的CommandArgument 属性上面!
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton rb = (LinkButton)e.Row.FindControl("LinkButton1");
if (rb != null)
rb.CommandArgument = e.Row.RowIndex.ToString();
//把选中行的RowIndex也传过去,提交后在服务器端取值时用
}
}
然后rowcommand事件里面这么写就找到啦!
Label lab1 = (Label)(GridView1.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("Label1"));
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton rb = (LinkButton)e.Row.FindControl("LinkButton1");
if (rb != null)
rb.CommandArgument = e.Row.RowIndex.ToString();
//把选中行的RowIndex也传过去,提交后在服务器端取值时用
}
}
然后rowcommand事件里面这么写就找到啦!
Label lab1 = (Label)(GridView1.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("Label1"));
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询