加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 03:43:10 所属栏目:asp.Net 来源:网络整理
导读:我正在使用嵌套的Gridview(我有5个嵌套的Gridviews). 我为这些Gridview应用了一个Regular Field验证器. 但是一旦我点击按钮,就会在文本框中生成逗号.因此,每当我点击按钮时,所有验证都会被触发. 我研究了很多文章,但没有找到与之相关的文章. 以下是Gridview
我正在使用嵌套的Gridview(我有5个嵌套的Gridviews).
我为这些Gridview应用了一个Regular Field验证器.

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

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

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

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.如何从文本框中删除逗号?

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

3.嵌套Gridview的限制?

任何帮助将不胜感激.

解决方法

如果您有重复的表单字段名称,则值将与逗号连接在一起.

所以,例如,如果你有以下..

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

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

name=,

这就是发生的事情.

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

1)UpdatePanel

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

2)在Page_Load()期间更改DataBind()行为

所以…

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

……但这并没有改变这些因素首先发布的事实.因此,如果您主要关注最终美学而不是行为,您可以使用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...
    });
});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读