asp.net 在GridView EditItemTemplate模版里获取控件

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Width
="100%" CellPadding="4" ForeColor="#333333" GridLines="None"
onrowediting
="GridView1_RowEditing" DataKeyNames="com_id"
onrowcommand
="GridView1_RowCommand"
onrowdatabound
="GridView1_RowDataBound"
onrowupdated
="GridView1_RowUpdated" onrowcreated="GridView1_RowCreated">
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:TemplateField HeaderText="编号">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"><%# Eval("com_num") %></asp:Label>

</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="公司名">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text="Label"><%# Eval("company_name") %></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="状态">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text="Label"><%# Eval("status") %></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>

<asp:CommandField ShowEditButton="True" HeaderText="编辑" />

<asp:BoundField />

</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>

我想在 RowDataBound事件里查找EditItemTemplate里的控件 动态的给空间赋值  可是怎么都找不到值

无语了 总是提示:未将对象因引用设置到对象的实力。

在网上的各种发放都试过了,每一个有用的  请高人帮一下忙  ,小弟现在没分了 ,有哪位高手愿意帮助一下, 谢谢了!

慕的地8271018
浏览 997回答 2
2回答

慕桂英546537

要想RowDataBound时间找到EditItemTemplate里的控件,必须点击编辑按钮执行   protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)        {            GridView1.EditIndex = e.NewEditIndex;            LoadData();//此时绑定数据源时RowDataBound事件函数能找到你的控件。            }  private void LoadData()        {            GridView1.DataSource = GridViewClass.ReadData();            GridView1.DataBind();        }         protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)        {            GridView1.EditIndex = -1;            LoadData();        }         protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)        {            if (e.Row.RowType == DataControlRowType.DataRow)            {               //要想找到控件必须点击EDIT按钮重新绑定DataSource                TextBox txt = (TextBox)e.Row.FindControl("TextBox1");                if (txt != null)                {                    txt.Text ="ggggg";                }            }        }

一只斗牛犬

模板列找控件关键FindControl (e.Row.FindControl(要找的控件ID) as Textbox).Text
打开App,查看更多内容
随时随地看视频慕课网APP