在GridView控件,项模板混乱的FindControl空值 [英] null value in findcontrol of gridview, item template confusion

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

问题描述

我有模板字段的GridView控件如下:

 < ASP:的TemplateField ShowHeader =FALSE>
            <&EditItemTemplate的GT;
            < ASP:文本框ID =txtEmpName=服务器>< / ASP:文本框>
                < ASP:文本框ID =txtBonus=服务器>< / ASP:文本框>
                < ASP:文本框ID =txtID=服务器>< / ASP:文本框>
            < / EditItemTemplate中>
                    <&EditItemTemplate的GT;
                        < ASP:LinkBut​​ton的ID =LinkBut​​ton1=服务器的CausesValidation =真
                            的CommandName =更新文本=更新>< / ASP:LinkBut​​ton的>
                        &安培; NBSP;< ASP:LinkBut​​ton的ID =LinkBut​​ton2=服务器的CausesValidation =FALSE
                            的CommandName =取消文本=取消>< / ASP:LinkBut​​ton的>
                    < / EditItemTemplate中>
                    <&ItemTemplate中GT;
                        < ASP:LinkBut​​ton的ID =LinkBut​​ton1=服务器的CausesValidation =FALSE
                            的CommandName =编辑文本=编辑>< / ASP:LinkBut​​ton的>                    < / ItemTemplate中>
                < / ASP:的TemplateField>

当我在 gv_RowUpdating 事件,我想通过的FindControl采取编辑字段的值。

有关,我使用以下code:

 `文本框txtUname =(文本框)gv.Rows [e.RowIndex] .FindControl(txtEmpName);`

但它显示我每次 txtUname 值当我调试code

可以是什么问题?

完整事件code:

 保护无效gv_RowUpdating(对象发件人,GridViewUpdateEventArgs E)
        {
            尝试
            {                文本框txtUname =(文本框)gv.Rows [e.RowIndex] .FindControl(txtEmpName);                浮动奖金= float.Parse(gv.DataKeys [e.RowIndex] .Values​​ [奖金]的ToString());                尝试
                {
                    CMD =新的SqlCommand(更新EMP集empName = @易名);
                    cmd.parameters.AddParametersWithValue(@易名,txtUname.Text);
                    cmd.ExecuteNonQuery();
                }
                赶上(异常前)
                {
                }
            }
            赶上(异常前)
            {
            }        }

修改

 保护无效的Page_Load(对象发件人,EventArgs的发送)
        {            CON =新的SqlConnection(数据源= 192.168.51.71;初始目录= WebBasedNewSoft;用户ID = SA;密码=帕布);            BindGrid();
        }
        私人无效BindGrid()
        {
            尝试
            {
                DA =新SqlDataAdapter的(选择EMP *,CON);
                DataSet的DS =新的DataSet();
                da.Fill(DS);
                GridView1.DataSource = ds.Tables [0];
                GridView1.DataBind();
            }
            赶上(异常前)
            {
            }
        }
        保护无效GridView1_RowUpdating(对象发件人,GridViewUpdateEventArgs E)
        {
            INT指数= GridView1.EditIndex;            GridViewRow行= GridView1.Rows [指数]            。串易名=((文本框)row.Cells [2] .Controls [0])Text.ToString()修剪();            尝试
            {
                con.Open();
                CMD =新的SqlCommand(更新设置EMP empName ='+易名+',CON);
                cmd.ExecuteNonQuery();
                con.Close();
            }
            赶上(异常前)
            {
            }        }        保护无效GridView1_RowEditing(对象发件人,GridViewEditEventArgs E)
        {
            GridView1.EditIndex = e.NewEditIndex;
            BindGrid();
        }
    }


解决方案

请参阅本

http://www.$c$cproject.com/Articles/37207/Editable-Gridview-with-Textbox-CheckBox-Radio-Butt

也把你的code编辑命令。它可能RowEditing或RowCommand

I have Template Field for gridview as below:

<asp:TemplateField ShowHeader="False">
            <EditItemTemplate>
            <asp:TextBox ID="txtEmpName" runat="server"></asp:TextBox>
                <asp:TextBox ID="txtBonus" runat="server"></asp:TextBox>
                <asp:TextBox ID="txtID" runat="server"></asp:TextBox>
            </EditItemTemplate>
                    <EditItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" 
                            CommandName="Update" Text="Update"></asp:LinkButton>
                        &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
                            CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
                            CommandName="Edit" Text="Edit"></asp:LinkButton>

                    </ItemTemplate>
                </asp:TemplateField>

When i am in gv_RowUpdating event, i wanted to take the value of edited field by findcontrol.

For that i am using following code:

`TextBox txtUname = (TextBox)gv.Rows[e.RowIndex].FindControl("txtEmpName");`

But each time it is showing me null value in txtUname when i debug the code.

What can be the problem?

Full Event Code:

 protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {

                TextBox txtUname = (TextBox)gv.Rows[e.RowIndex].FindControl("txtEmpName");

                float bonus = float.Parse(gv.DataKeys[e.RowIndex].Values["bonus"].ToString());

                try
                {
                    cmd = new SqlCommand("update emp set empName=@eName");
                    cmd.parameters.AddParametersWithValue("@eName",txtUname.Text);
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                }
            }
            catch (Exception ex)
            {
            }

        }

EDIT

protected void Page_Load(object sender, EventArgs e)
        {

            con = new SqlConnection("Data Source=192.168.51.71;Initial Catalog=WebBasedNewSoft;User ID=sa;password=prabhu");

            BindGrid();
        }
        private void BindGrid()
        {
            try
            {
                da = new SqlDataAdapter("select * from emp", con);
                DataSet ds = new DataSet();
                da.Fill(ds);
                GridView1.DataSource = ds.Tables[0];
                GridView1.DataBind();
            }
            catch (Exception ex)
            {
            }
        }
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int index = GridView1.EditIndex;

            GridViewRow row = GridView1.Rows[index];

            string eName = ((TextBox)row.Cells[2].Controls[0]).Text.ToString().Trim();

            try
            {
                con.Open();
                cmd = new SqlCommand("update emp set empName='"+eName+"'",con);
                cmd.ExecuteNonQuery();
                con.Close();
            }
            catch(Exception ex)
            {
            }

        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            BindGrid();
        }
    }

解决方案

refer this

http://www.codeproject.com/Articles/37207/Editable-Gridview-with-Textbox-CheckBox-Radio-Butt

also put your code for Edit command. it might be RowEditing or RowCommand

这篇关于在GridView控件,项模板混乱的FindControl空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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