首页 新闻 会员 周边

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

0
[已解决问题] 解决于 2012-06-21 10:39
<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里的控件 动态的给空间赋值  可是怎么都找不到值

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

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

JIM.WEN的主页 JIM.WEN | 初学一级 | 园豆:14
提问于:2011-05-31 22:37
< >
分享
最佳答案
0

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

奖励园豆:5
yxf2011 | 初学一级 |园豆:6 | 2011-06-01 13:08
其他回答(1)
0

模板列找控件关键FindControl

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

TheEnd | 园豆:225 (菜鸟二级) | 2011-06-03 11:48
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册