在GridView的编辑模板变更控制的价值 [英] Change value of control in GridView edit template

查看:108
本文介绍了在GridView的编辑模板变更控制的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GridView和我需要能够以编程方式修改编辑模板的文本框的值。当我试图onRowDataBound期间访问它,我得到这样的:

异常详细信息:System.NullReferenceException:未将对象引用设置到对象的实例

我以为onRowDataBound方法中,编辑模板控件不可访问。当我试图编辑在onRowEditing方法文本框的值,但是,没有.Row选项的GridViewEditEventArgs反对,所以看来你不能访问控件编辑模板中的onRowEditing方法无论是。

任何想法如何以编程方式更改在GridView的编辑模板的文本框的值?

ASP.NET的WebForms,.NET 4.0,C#

====

编辑#1:这是我现在有。该txtMiles对象最终被在RowEditing空。

 < ASP:模板列HEADERTEXT =万里频率>
                    <的ItemTemplate>
                        < ASP:标签ID =lblFreqMiles=服务器文本='<%#的eval(FrequencyMiles)%>'>< / ASP:标签>
                    < / ItemTemplate中>
                    < EditItemTemplate中>
                        < ASP:文本框ID =txtFreqMiles=服务器文本='<%#的eval(FrequencyMiles)%> WIDTH =50像素>< / ASP:文本框>
                        < ASP:使用RequiredFieldValidator =服务器ID =REQ1的ControlToValidate =txtFreqMiles的ErrorMessage =*/>
                        < ASP:CompareValidator控件ID =CompareValidator1=服务器运算符=DataTypeCheck类型=整数的ControlToValidate =txtFreqMiles的ErrorMessage =值必须是整数。 />
                    < / EditItemTemplate中>
                < / ASP:的TemplateField>


    保护无效gvMaint_RowEditing(对象发件人,GridViewEditEventArgs E)
            {
                //格式万里频率列
                GridViewRow行= grvMaint.Rows [e.NewEditIndex]
                文本框txtMiles =(文本框)row.FindControl(txtFreqMiles);
                如果(txtMiles.Text ==999999)
                {
                    //做的东西
                }

                grvMaint.EditIndex = e.NewEditIndex;
                populateMaintGrid();
            }
 

解决方案

只要确保该行处于编辑模式,然后再尝试,并得到了控制:

 保护无效gvMaint_RowDataBound(对象发件人,GridViewRowEventArgs E)
{
    如果(e.Row.RowState == DataControlRowState.Edit)
    {
        文本框txtFreqMiles =(文本框)e.Row.FindControl(txtFreqMiles);

        //在这一点上,你可以更改值正常
        txtFreqMiles.Text =一些新的案文;
    }
}
 

I've got a GridView and I need to be able to programmatically change the value of a TextBox in the edit template. When I attempt to access it during onRowDataBound I get this:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

I am thinking that inside the onRowDataBound method, the edit template controls are not accessible. When I attempt to edit the TextBox's value in the onRowEditing method, however, there is no .Row option for the GridViewEditEventArgs object, so it appears that you can't access controls in the edit template in the onRowEditing method either.

Any ideas how to programmatically change the value of a TextBox in a GridView's edit template?

ASP.NET WebForms, .NET 4.0, C#

====

Edit #1: This is what I currently have. The txtMiles object ends up being null in RowEditing.

<asp:TemplateField HeaderText="Miles Frequency">
                    <ItemTemplate>
                        <asp:Label ID="lblFreqMiles" runat="server" Text='<%# Eval("FrequencyMiles") %>'></asp:Label>
                    </ItemTemplate>                    
                    <EditItemTemplate >
                        <asp:TextBox ID="txtFreqMiles" runat="server" Text='<%# Eval("FrequencyMiles")%>'  Width="50px"></asp:TextBox>
                        <asp:RequiredFieldValidator runat="server" ID="req1" ControlToValidate="txtFreqMiles" ErrorMessage="*" />
                        <asp:CompareValidator ID="CompareValidator1" runat="server" Operator="DataTypeCheck" Type="Integer" ControlToValidate="txtFreqMiles" ErrorMessage="Value must be a whole number." />
                    </EditItemTemplate>
                </asp:TemplateField>


    protected void gvMaint_RowEditing(object sender, GridViewEditEventArgs e)
            {
                //format Miles Frequency column
                GridViewRow row = grvMaint.Rows[e.NewEditIndex];
                TextBox txtMiles = (TextBox)row.FindControl("txtFreqMiles");
                if (txtMiles.Text == "999999")
                {
                    //do stuff
                }

                grvMaint.EditIndex = e.NewEditIndex;
                populateMaintGrid();
            }

解决方案

Just make sure the row is in edit mode before you try and get the control:

protected void gvMaint_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowState == DataControlRowState.Edit)
    {
        TextBox txtFreqMiles = (TextBox)e.Row.FindControl("txtFreqMiles");

        // At this point, you can change the value as normal
        txtFreqMiles.Text = "some new text";
    }
}

这篇关于在GridView的编辑模板变更控制的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆