在gridvew footertemplet中进行验证 [英] Validation in the gridvew footertemplet

查看:51
本文介绍了在gridvew footertemplet中进行验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个GridView,我想添加一行FooterTemplate在GridView中添加新记录.但是我想在FooterTemplate中验证TextBoxs.我使用linkbutton进行添加.我使用的语言是C#,Web Form.

谢谢

Hi
I have a GridView , I want add a row FooterTemplate to add a new record in GridView . But I want to validate TextBoxs in FooterTemplate.I use a linkbutton for adding. My language I using is C# , Web Form .

Thanks

推荐答案

嗨 看看这个例子

Hi Have a look on this example

<script>
                function Validate(txtNameId) {
                        if (document.getElementById(txtNameId).value.length > 0)
                                return true;
                        alert('Name should not be empty');
                        return false;
                }
</script>


</head>
<body>
        <form id="form1" runat="server">
        <asp:GridView ID="GridView1" AutoGenerateColumns="false" ShowFooter="true" runat="server">
                <Columns>
                        <asp:TemplateField HeaderText="Id">
                                <ItemTemplate>
                                        <asp:Label ID="lblId" runat="server" Text='<%# Eval("ID" )%>'></asp:Label>
                                </ItemTemplate>
                                <FooterTemplate>
                                        <asp:TextBox ID="txtId" runat="server"></asp:TextBox>
                                </FooterTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Name">
                                <ItemTemplate>
                                        <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name" )%>'></asp:Label>
                                </ItemTemplate>
                                <FooterTemplate>
                                        <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                                        &nbsp;&nbsp;
                                        <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
                                </FooterTemplate>
                        </asp:TemplateField>
                </Columns>
        </asp:GridView>
        </form>


protected void Page_Load(object sender, EventArgs e)
        {
                if (!Page.IsPostBack)
                {
                        this.BindGrid();
                }
        }



protected void btnAdd_Click(object sender, EventArgs e)
       {
               DataTable dt = GetDT();
               String id = (this.GridView1.FooterRow.FindControl("txtId") as TextBox).Text;
               String Name = (this.GridView1.FooterRow.FindControl("txtName") as TextBox).Text;
               dt.Rows.Add(new Object[] { id, Name });
               ViewState["DT"] = dt;
               this.BindGrid();
       }



private void BindGrid()
        {
                DataTable dt = GetDT();
                this.GridView1.DataSource = dt;
                this.GridView1.DataBind();
                Button btnAdd = (this.GridView1.FooterRow.FindControl("btnAdd") as Button);
                btnAdd.Attributes.Add("onclick", "return Validate('" + this.GridView1.FooterRow.FindControl("txtName").ClientID + "')");
        }



private DataTable GetDT()
        {
                if (ViewState["DT"] == null)
                {
                        DataTable dt = new DataTable();
                        dt.Columns.Add("Id");
                        dt.Columns.Add("Name");
                        ViewState["DT"] = dt;
                        dt.Rows.Add(dt.NewRow());
                        return dt;
                }
                return ViewState["DT"] as DataTable;
        }


引用该线程
http: //stackstackflow.com/questions/9224854/how-to-keep-the-textbox-values-in-asp-net-gridview-after-checking-the-required-f [
Refer this thread
http://stackoverflow.com/questions/9224854/how-to-keep-the-textbox-values-in-asp-net-gridview-after-checking-the-required-f[^]


这篇关于在gridvew footertemplet中进行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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