文本框导致ASP.NET C#SQL出现问题

时间:2014-10-07 07:33:01

标签: c# asp.net gridview nested

我正在使用嵌套的Gridview(我有5个嵌套的Gridviews)。 我为这些Gridviews应用了一个常规字段验证器。

但是一旦我点击按钮,就会在文本框中生成逗号。因此,每当我点击按钮时,所有验证都会被解雇。

我研究过很多文章,但没有找到与之相关的文章。

以下是Gridview的图像,其中包含在文本框中生成的逗号:

Image Of Error Page

HTML代码部分

<%-- First Gridview--%>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
    CssClass="gvstyling gridview_width_60" ShowHeaderWhenEmpty="true" EmptyDataText="Record(s) Not Found!"
    DataKeyNames="locality" ShowHeader="false" OnRowDataBound="gvLocality_RowDataBound">
    <Columns>

        <asp:TemplateField ItemStyle-Width="15px">
            <ItemTemplate>
                <img alt="" style="cursor: pointer" src="../images/plus.png" />

                <asp:Panel ID="pnlCompanyName" runat="server" Style="display: none">

                    <%-- Second Gridview --%>
                    <asp:GridView ID="gvCompanyName" ShowHeader="false" ShowHeaderWhenEmpty="false" CssClass="gvstyling gridview_width_100"
                        OnRowDataBound="gvCompanyName_RowDataBound" runat="server"
                        AutoGenerateColumns="false" EmptyDataText="No Record(s) Found!">

                        <Columns>

                            <asp:TemplateField ItemStyle-Width="15px">
                                <ItemTemplate>
                                    <asp:HiddenField ID="hfRetailer_Id" Value='<%# Eval("retailer_id") %>' runat="server"></asp:HiddenField>
                                    <asp:HiddenField ID="hfLocality" Value='<%# Eval("locality") %>' runat="server"></asp:HiddenField>
                                    <asp:Label ID="lbl" Visible="false" Text='<%# Eval("retailer_id") %>' runat="server"></asp:Label>
                                    <img alt="" style="cursor: pointer" src="../images/plus.png" />

                                    <asp:Panel ID="pnlSellOrderNo" runat="server" Style="display: none">

                                        <%-- Third Gridview --%>
                                        <asp:GridView ID="gvSellOrderNo" ShowHeader="false" ShowHeaderWhenEmpty="false"
                                            CssClass="gvstyling gridview_width_100" runat="server" OnRowDataBound="gvSellOrderNo_RowDataBound"
                                            AutoGenerateColumns="false" EmptyDataText="No Record(s) Found!">
                                            <Columns>
                                                <asp:TemplateField ItemStyle-Width="15px">
                                                    <ItemTemplate>
                                                        <asp:HiddenField ID="hf_SellOrderNo" Value='<%# Eval("sell_order_no") %>' runat="server"></asp:HiddenField>
                                                        <asp:Label ID="lblSellOrderNo" Visible="false" Text='<%# Eval("sell_order_no") %>' runat="server"></asp:Label>
                                                        <img alt="" style="cursor: pointer" src="../images/plus.png" />
                                                        <asp:Panel ID="pnlProductDetails" runat="server" Style="display: none">

                                                            <%-- fourth Gridview --%>
                                                            <asp:GridView ID="gvProductDetails" ShowHeader="false" ShowHeaderWhenEmpty="false"
                                                                CssClass="gvstyling gridview_width_100" runat="server" OnRowDataBound="gvProductDetails_RowDataBound"
                                                                AutoGenerateColumns="false" EmptyDataText="No Record(s) Found!">
                                                                <Columns>

                                                                    <asp:TemplateField ItemStyle-Width="15px">
                                                                        <ItemTemplate>
                                                                            <asp:HiddenField ID="HiddenField1" Value='<%# Eval("sell_order_no") %>' runat="server"></asp:HiddenField>
                                                                            <asp:HiddenField ID="hfProductId" Value='<%# Eval("product_id") %>' runat="server"></asp:HiddenField>
                                                                            <asp:Label ID="lblProductId" Visible="false" Text='<%# Eval("product_id") %>' runat="server"></asp:Label>

                                                                            <img alt="" style="cursor: pointer" src="../images/plus.png" />
                                                                            <asp:Panel ID="pnlWarehouseDetails" runat="server" Style="display: none">

                                                                                <%-- fifth Gridview--%>
                                                                                <asp:GridView ID="gvWarehouseDetails" ShowHeader="false" ShowHeaderWhenEmpty="false"
                                                                                    CssClass="gvstyling gridview_width_100" runat="server"
                                                                                    AutoGenerateColumns="false">

                                                                                    <Columns>
                                                                                        <asp:TemplateField>
                                                                                            <ItemTemplate>
                                                                                                <asp:Label ID="lblWarehouseId" Text='<%# Eval("c_warehouse_id") %>' Visible="false" runat="server"></asp:Label>
                                                                                                <%# Eval("warehouse_name") %> (<%# Eval("qty") %>)
                                                                                            </ItemTemplate>
                                                                                        </asp:TemplateField>

                                                                                        <asp:TemplateField>
                                                                                            <ItemTemplate>
                                                                                                <asp:TextBox ID="txtQty" runat="server"></asp:TextBox>
                                                                                            </ItemTemplate>
                                                                                        </asp:TemplateField>
                                                                                    </Columns>

                                                                                </asp:GridView>

                                                                                <%-- fifth Gridview --%>
                                                                            </asp:Panel>
                                                                        </ItemTemplate>
                                                                    </asp:TemplateField>



                                                                    <asp:TemplateField>
                                                                        <ItemTemplate>
                                                                            <%# Eval("product_name") %> (<%# Eval("qty") %>)
                                                                        </ItemTemplate>
                                                                    </asp:TemplateField>
                                                                </Columns>
                                                            </asp:GridView>
                                                            <%-- fourth Gridview --%>
                                                        </asp:Panel>
                                                    </ItemTemplate>
                                                </asp:TemplateField>

                                                <asp:TemplateField>
                                                    <ItemTemplate>
                                                        <%# Eval("sell_order_no") %>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                            </Columns>
                                        </asp:GridView>
                                        <%-- Third Gridview --%>
                                    </asp:Panel>
                                </ItemTemplate>
                            </asp:TemplateField>

                            <asp:TemplateField HeaderText="Business Name">
                                <ItemTemplate>
                                    <%# Eval("business_name") %>
                                </ItemTemplate>
                            </asp:TemplateField>

                        </Columns>

                    </asp:GridView>
                    <%-- Second  Gridview--%>
                </asp:Panel>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField ItemStyle-CssClass="gv_item_bg">
            <ItemTemplate>
                <asp:Label ID="lblLocality" runat="server" Text=' <%# Eval("locality") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

    </Columns>
</asp:GridView>
<%-- First Gridview --%>

文件后面的代码

//Filling Shipping Company Name
private void FillShippingCompanyName()
{
    try
    {
        ArrayList arr = new ArrayList();
        cm.ds.Clear();
        cm.sp_dataset_execute("spdisplay_Shipping_Comany_Name", arr);

        ddlCompanyName.DataSource = cm.ds;
        ddlCompanyName.DataValueField = "shipping_code";
        ddlCompanyName.DataTextField = "shipping_name";
        ddlCompanyName.DataBind();
        ddlCompanyName.Items.Insert(0, new ListItem("---- Select Shipping Company ----", "0"));
    }
    catch (Exception ex)
    {
        ErrHandler.WriteError(ex.Message.ToString(), "Shipping-Order-FillShippingCompanyName()");
    }
    finally
    {
        cm.con.Close();
    }

}

private void FillLocality()
{
    try
    {
        cm.ds.Clear();
        ArrayList arr = new ArrayList();
        cm.sp_dataset_execute("spDisplay_Locality", arr);

        gvLocality.DataSource = cm.ds;
        gvLocality.DataBind();
    }
    catch (Exception ex)
    {
        ErrHandler.WriteError(ex.Message.ToString(), "Shipping-Order-FillLocality()");
        cm.con.Close();
    }
    finally
    {
        cm.con.Close();
    }
}

//Locality Gridview Row Databound (Level 1 Grdview)
common cm1 = new common();
protected void gvLocality_RowDataBound(object sender, GridViewRowEventArgs e)
{
    try
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string Locality = gvLocality.DataKeys[e.Row.RowIndex].Value.ToString();
            GridView gvCompanyName = e.Row.FindControl("gvCompanyName") as GridView;

            cm1.ds.Clear();
            //Binding Company Gridview
            ArrayList arr1 = new ArrayList();
            arr1.Add("@locality|" + Locality + "");
            cm1.sp_dataset_execute("spDisplayCompanyName", arr1);

            gvCompanyName.DataSource = cm1.ds;
            gvCompanyName.DataBind();
        }
    }
    catch (Exception ex)
    {
        ErrHandler.WriteError(ex.Message.ToString(), "Recent_activity-gvRecentActivityOuter_RowDataBound()");
        cm1.con.Close();
    }
    finally
    {
        cm1.con.Close();
    }
}

//Comapny Name Gridview Row Databound (Level 2 Grdview)
common cm2 = new common();
protected void gvCompanyName_RowDataBound(object sender, GridViewRowEventArgs e)
{
    try
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string RetailerId = ((HiddenField)e.Row.FindControl("hfRetailer_Id")).Value;
            GridView gvSellOrderNo = e.Row.FindControl("gvSellOrderNo") as GridView;
            string Locality = ((HiddenField)e.Row.FindControl("hfLocality")).Value;



            cm2.ds.Clear();
            //Binding Company Gridview
            ArrayList arr = new ArrayList();
            arr.Add("@retailer_id|" + RetailerId + "");
            arr.Add("@locality|" + Locality + "");
            cm2.sp_dataset_execute("spDisplay_SellOrderNo", arr);

            gvSellOrderNo.DataSource = cm2.ds;
            gvSellOrderNo.DataBind();
        }
    }
    catch (Exception ex)
    {
        ErrHandler.WriteError(ex.Message.ToString(), "Shipping-Order-Page_Load()");
        cm2.con.Close();
    }
    finally
    {
        cm2.con.Close();
    }
}

//Sell Order Gridview Row Databound (Level 3 Grdview)
common cm3 = new common();
protected void gvSellOrderNo_RowDataBound(object sender, GridViewRowEventArgs e)
{
    try
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string SellOrderNo = ((HiddenField)e.Row.FindControl("hf_SellOrderNo")).Value;
            GridView gvProductDetails = e.Row.FindControl("gvProductDetails") as GridView;

            FillProductDetails(gvProductDetails, SellOrderNo);
        }
    }
    catch (Exception ex)
    {
        ErrHandler.WriteError(ex.Message.ToString(), "Shipping-Order-gvSellOrderNo_RowDataBound()");
        cm3.con.Close();
    }
    finally
    {
        cm3.con.Close();
    }
}

private void FillProductDetails(GridView gvProductDetails, string SellOrderNo)
{

    cm3.ds.Clear();
    //Product Details Gridview
    ArrayList arr = new ArrayList();
    arr.Add("@sell_order_no|" + SellOrderNo + "");

    cm3.sp_dataset_execute("spDisplay_ProductDetails", arr);

    gvProductDetails.DataSource = cm3.ds;
    gvProductDetails.DataBind();
}

//Product Details Gridview Row Databound (Level 4 Grdview)
common cm4 = new common();
protected void gvProductDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
    try
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string ProductID = ((HiddenField)e.Row.FindControl("hfProductId")).Value;

            string SellOrderNo = ((HiddenField)e.Row.FindControl("HiddenField1")).Value;

            GridView gvWarehouseDetails = e.Row.FindControl("gvWarehouseDetails") as GridView;

            cm4.ds.Clear();
            //Product Details Gridview
            ArrayList arr = new ArrayList();
            arr.Add("@product_id|" + ProductID + "");
            arr.Add("@sell_order_no|" + SellOrderNo + "");

            cm4.sp_dataset_execute("spDisplay_WarehouseDetails", arr);

            gvWarehouseDetails.DataSource = cm4.ds;
            gvWarehouseDetails.DataBind();
        }
    }
    catch (Exception ex)
    {
        ErrHandler.WriteError(ex.Message.ToString(), "Shipping-Order-gvProductDetails_RowDataBound()");
        cm4.con.Close();
    }
    finally
    {
        cm4.con.Close();
    }
}

//Submit Button
protected void btnSubmit_Click1(object sender, EventArgs e)
{
    try
    {
        cm.ds.Clear();
        //--Insert Query for Rs_Shipping_Order_Details
        string ShippingCode = "SHO" + DateTime.Now.ToString("yyyyMMddHHmmss");

        string ShippingGroup = "SG" + DateTime.Now.ToString("yyyyMMddHHmmss");

        ArrayList arr = new ArrayList();
        arr.Add("@shipping_code|" + ShippingCode + "");
        arr.Add("@shipping_Company_code|" + ddlCompanyName.SelectedValue + "");
        arr.Add("@is_shipping_delivered|0");
        cm.sp_execute("spInsert_Shipping_Order_Detail", arr);

        //Locality for loop
        for (int i = 0; i < gvLocality.Rows.Count; i++)
        {
            GridView gvCompanyName = gvLocality.Rows[i].FindControl("gvCompanyName") as GridView;

            //Company for loop
            for (int j = 0; j < gvCompanyName.Rows.Count; j++)
            {
                GridView gvSellOrderNo = gvCompanyName.Rows[j].FindControl("gvSellOrderNo") as GridView;

                string RetailerId = ((Label)gvCompanyName.Rows[j].FindControl("lbl")).Text;


                //Sell Order for loop
                for (int k = 0; k < gvSellOrderNo.Rows.Count; k++)
                {
                    //Product Details Gridview
                    GridView gvProductDetails = gvSellOrderNo.Rows[k].FindControl("gvProductDetails") as GridView;
                    string SO = ((Label)gvSellOrderNo.Rows[k].FindControl("lblSellOrderNo")).Text;
                    int retailer_Id = Convert.ToInt32(RetailerId);

                    //Product Details for loop
                    for (int l = 0; l < gvProductDetails.Rows.Count; l++)
                    {

                        //Warehouse Details Gridview
                        GridView gvWarehouseDetails = gvProductDetails.Rows[l].FindControl("gvWarehouseDetails") as GridView;

                        string ProductId = ((Label)gvProductDetails.Rows[l].FindControl("lblProductId")).Text;

                        //Warehouse Details for loop
                        for (int m = 0; m < gvWarehouseDetails.Rows.Count; m++)
                        {
                            TextBox txtQty = gvWarehouseDetails.Rows[m].FindControl("txtQty") as TextBox;
                            string LastValue = txtQty.Text.Split(',').Last();

                            if (String.IsNullOrEmpty(LastValue) == false)
                            {
                                string Warehouse_Id = ((Label)gvWarehouseDetails.Rows[m].FindControl("lblWarehouseId")).Text;
                                int warehouse_id = Convert.ToInt32(Warehouse_Id);
                                string Qty = LastValue;

                                //Insert Query for Rs_Shipping_Detail_Mapping
                                ArrayList arr1 = new ArrayList();
                                arr1.Add("@shipping_order_code|" + ShippingCode + "");
                                arr1.Add("@retailer_id|" + retailer_Id + "");
                                arr1.Add("@sell_order_no|" + SO + "");
                                arr1.Add("@product_id|" + ProductId + "");
                                arr1.Add("@c_warehouse_id|" + warehouse_id + "");
                                arr1.Add("@shipping_group|" + ShippingGroup + "");
                                arr1.Add("@qty|" + Qty + "");

                                common cm1 = new common();
                                cm1.sp_execute("spInsert_Shipping_Detail_Mapping", arr1);


                                ////Generating Pdf for Each Sell Order
                                //if (m == gvWarehouseDetails.Rows.Count - 1)
                                //{
                                //    Generate_SellOrderWise_PDf(SO, ShippingCode, ShippingGroup);
                                //}
                            }
                        }
                    }
                }
            }
        }
        Response.Redirect("../final-shipping-order/?SG=" + ShippingGroup, false);
    }
    catch (Exception ex)
    {
        ErrHandler.WriteError(ex.Message.ToString(), "Shipping-Order-btnSubmit_Click1()");
        cm4.con.Close();
    }
    finally
    {
        cm4.con.Close();
    }
}    

问题:

1。如何从文本框中删除逗号?

2。这些逗号的原因是什么? (为什么点击按钮时会生成逗号?)

第3。嵌套网格视图的限制?

任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:5)

如果您有重复的表单字段names,则这些值将以逗号连接在一起。

因此,例如,如果您有以下内容..

<input type="text" name="name" value="">
<input type="text" name="name" value="">

..您在Request.Form回发上的结果值如下所示:

name=,,

发生了什么。

以下是您的问题的一些可能的解决方案,但我还没有测试过它们:)

1)UpdatePanel

从阅读开始,似乎如果为违规网格控件(&lt; asp:TextBox ID="txtQty" runat="server"></asp:TextBox>)创建UpdatePanel,它就会消除此问题。我再次测试过这个

2)在Page_Load()

期间更改DataBind()行为

因此...

page_load()
{
    if(!isPostBack())
    {
        // DataBind normally
        myGridview.DataBind();
    }
    else
    {
        //Some intelligent way to remove commas before binding
    }
}

......但这并没有改变,,,values首先发布的事实。因此,如果您主要关注最终美学而不是行为,您可以使用JS去除逗号(如之前建议的那样)。

3)JS - 摆脱逗号:

(如建议here

<script type="text/javascript">
    $("[src*=plus]").live("click", function () {
        $(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>")
        $(this).attr("src", "images/minus.png");
        $("input", $(this).closest("tr").next()).each(function () {
            this.value = this.value.substring(',', '');
        });
    });
    $("[src*=minus]").live("click", function () {
        $(this).attr("src", "images/plus.png");
        $(this).closest("tr").next().remove();
    });
</script>

希望这会有所帮助:)

[编辑] - 验证

我会使用.keydown()事件触发验证。在您的情况下,它可能看起来像这样:

// Bind to each input with id='txtQty', in each row, in the "gridview" with id='gvWarehouseDetails'
 $("#gvWarehouseDetails tr input[id*='txtQty']").each(function () {
    $(this).keydown(function (event) { // <-- specifies the specific input
        // Validation logic goes here...
    });
});

答案 1 :(得分:4)

Ajax Control Toolkit 是一个可行的解决方案,但对它的支持已经不存在了。我假设该字段上有动态控制级别。从本质上讲,网格会在某些情况下显示它吗?

无论哪种方式,您都可以在字段中添加特定的class

<asp:Textbox id="txtContent" runat="server" CssClass="Validator">

基本上,当网格将所有这些字段添加到您的网页时,他们都会拥有Validator class。因此,您可以编写JavaScript以实际将字符剥离出字段,例如:

$('.Validator').on('blur', function() {
     $(this).replace(',', '');
});

只要焦点发生变化,只要鼠标离开,,该字段就会从字段中移除,,并从字段中删除。

这是一种方法,确实存在无数其他选项来实现这一目标。这个解决方案非常简单,灵活,轻便,因此应该足够了。

<强>更新


我选择的解决方案不应该在这些字段上进行循环。只是因为blur会在该字段上丢失changefocus时触发。由于您提到用户在字段中点击时会显示,。否则,您可以对所有字段进行验证。你只需要做的就是:

$('.Validator').each(function() {
     // Will iterate through each field.
});

因此,您可以自己使用JavaScript来验证这些字段,或者使用一个简单的库,例如Valid8,它将执行所有客户端验证。 没有回发,在发送到服务器进行处理之前都已完成客户端。

<强>避免:

  • 更新面板 - 这些都非常痛苦且非常低效。它们的工作方式基本上是通过获取整个页面并存储内存然后执行Ajax请求并刷新页面并将所有内容从内存中拉出以放置在页面上。此外,它使得使用Asp.Net 页面生命周期非常困难。

答案 2 :(得分:3)

Hardik,txtQty.Text一开始怎么样? 它是否为空文本框?

您使用代码string LastValue = txtQty.Text.Split(',').Last();尝试去除插入的逗号值或绑定的值带有逗号?我的意思是,您在数据库和应用程序中使用相同的UI文化,或者在显示之前是否必须对其进行格式化?

答案 3 :(得分:3)

我不知道您的代码中发生了什么。但如果你想摆脱逗号(&#39;,&#39;)那么我应该向你推荐一件事。

添加AjaxControlToolKit的引用并在 aspx 页面中注册,如下所示:

<%@ Register TagPrefix="ajaxToolkit" Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit, Version=[VersionNumber], Culture=neutral, PublicKeyToken=[TokenNumber]" %>

或者您可以参考How to install AJAX Control toolkit

安装了AJAX Control Toolkit后,转到您的页面,在TextBox txtQty 下面的网格TemplateField中,添加 FilteredTextBoxExtender 如下所示:

<asp:TemplateField>
    <ItemTemplate>
        <asp:TextBox ID="txtQty" runat="server"></asp:TextBox>
        <ajaxToolkit:FilteredTextBoxExtender runat="server" InvalidChars="," FilterMode="InvalidChars" TargetControlID="txtQty" />
    </ItemTemplate>
</asp:TemplateField>

通过添加FilterTextBoxExtender,它不会允许插入字符 InvalidChars 到文本框&#39; txtQty&#39;。