为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕的地8271018 2018-12-06 21:12:31
<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里的控件 动态的给空间赋值  可是怎么都找不到值 无语了 总是提示:未将对象因引用设置到对象的实力。 在网上的各种发放都试过了,每一个有用的  请高人帮一下忙  ,小弟现在没分了 ,有哪位高手愿意帮助一下, 谢谢了!
查看完整描述

2 回答

?
慕桂英546537

TA贡献1848条经验 获得超10个赞

要想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";
                }
            }
        }

查看完整回答
反对 回复 2019-01-21
?
一只斗牛犬

TA贡献1784条经验 获得超2个赞

模板列找控件关键FindControl

(e.Row.FindControl(要找的控件ID) as Textbox).Text

查看完整回答
反对 回复 2019-01-21
  • 2 回答
  • 0 关注
  • 995 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信