簡體   English   中英

所選索引中網格中文本框的Asp.Net更改值已更改

[英]Asp.Net change value of textbox in grid on selected Index changed

在此輸入圖像描述

我有這個網格,我動態添加控件。 現在我想更改產品選擇索引更改事件的成本價格和銷售價格的相應值。 問題是我無法獲得具體的行。 在我到目前為止的代碼下面。

    <asp:GridView ID="grdSales" runat="server"
                    ShowFooter="True" AutoGenerateColumns="False"
                    CellPadding="4" CssClass="table table-hover table-striped"
                    GridLines="None" OnRowDeleting="grdSales_RowDeleting" OnRowCommand="grdSales_RowCommand">
                    <Columns>
                        <asp:BoundField DataField="RowNumber" HeaderText="SNo" />

                        <asp:TemplateField HeaderText="Products">
                            <ItemTemplate>
                                <asp:DropDownList ID="ddlProducts" runat="server"  CssClass="grid-control form-control" OnSelectedIndexChanged="ddlProducts_SelectedIndexChanged">
                                </asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>


                        <asp:TemplateField HeaderText="Cost Price">
                            <ItemTemplate>
                                <asp:TextBox ID="txtCp" runat="server" CssClass="grid-control form-control"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                          <asp:TemplateField HeaderText="Sale Price">
                            <ItemTemplate>
                                <asp:TextBox ID="txtSp" runat="server" CssClass="grid-control form-control"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Total Units">
                            <ItemTemplate>
                                <asp:TextBox ID="txtUnits" runat="server" CssClass="grid-control form-control"></asp:TextBox>
                            </ItemTemplate>
                              <FooterStyle HorizontalAlign="Left" />
                            <FooterTemplate>
                               Total Journal Cost: <asp:Label ID="lblTotalCost" runat="server" Text="0"></asp:Label>
                            </FooterTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Total Cost">
                            <ItemTemplate>
                                <asp:TextBox ID="txtTotal" runat="server" CssClass="grid-control form-control"></asp:TextBox>
                            </ItemTemplate>




                             <FooterStyle HorizontalAlign="Left" />
                            <FooterTemplate>
                                <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" CssClass="btn btn-primary" OnClick="ButtonAdd_Click" />
                            </FooterTemplate>
                        </asp:TemplateField>

                        <asp:CommandField
                            ShowDeleteButton="True" />
                    </Columns>


                </asp:GridView>

而我的代碼背后是

 #region Private Functions
    private void FirstGridViewRow()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));

        dt.Columns.Add(new DataColumn("Products", typeof(string)));

        dt.Columns.Add(new DataColumn("CostPrice", typeof(string)));
        dt.Columns.Add(new DataColumn("SalePrice", typeof(string)));
        dt.Columns.Add(new DataColumn("TotalUnits", typeof(string)));
        dt.Columns.Add(new DataColumn("TotalCost", typeof(string)));
        dr = dt.NewRow();

        dr["RowNumber"] = 1;

        dr["Products"] = string.Empty;

        dr["CostPrice"] = string.Empty;
        dr["SalePrice"] = string.Empty;
        dr["TotalUnits"] = string.Empty;
        dr["TotalCost"] = string.Empty;


        dt.Rows.Add(dr);

        ViewState["CurrentTable"] = dt;

        grdSales.DataSource = dt;
        grdSales.DataBind();
        AddNewRow();
        SetRowData();
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtOld = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            int rowIndex = Convert.ToInt32(0);
            if (dtOld.Rows.Count > 1)
            {
                dtOld.Rows.Remove(dt.Rows[rowIndex]);
                drCurrentRow = dtOld.NewRow();
                ViewState["CurrentTable"] = dtOld;
                grdSales.DataSource = dtOld;
                grdSales.DataBind();

                for (int i = 0; i < grdSales.Rows.Count - 1; i++)
                {
                    grdSales.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
                }
                SetPreviousData();
            }
        }

    }
    private void AddNewRow()
    {
        int rowIndex = 0;

        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {

                    DropDownList ddlProducts =
                      (DropDownList)grdSales.Rows[rowIndex].Cells[1].FindControl("ddlProducts");

                    TextBox txtCostPrice =
                      (TextBox)grdSales.Rows[rowIndex].Cells[2].FindControl("txtCp");
                    TextBox txtSalePrice =
                     (TextBox)grdSales.Rows[rowIndex].Cells[3].FindControl("txtSp");
                    TextBox txtUnits =
                     (TextBox)grdSales.Rows[rowIndex].Cells[4].FindControl("txtUnits");
                    TextBox txtTotal =
                     (TextBox)grdSales.Rows[rowIndex].Cells[5].FindControl("txtTotal");

                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;


                    dtCurrentTable.Rows[i - 1]["Products"] = ddlProducts.SelectedValue;

                    dtCurrentTable.Rows[i - 1]["CostPrice"] = txtCostPrice.Text;
                    dtCurrentTable.Rows[i - 1]["SalePrice"] = txtSalePrice.Text;
                    dtCurrentTable.Rows[i - 1]["TotalUnits"] = txtUnits.Text;
                    dtCurrentTable.Rows[i - 1]["TotalCost"] = txtTotal.Text;


                    rowIndex++;
                }
                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["CurrentTable"] = dtCurrentTable;

                grdSales.DataSource = dtCurrentTable;
                grdSales.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }
        SetPreviousData();
    }
    private void SetPreviousData()
    {
        int rowIndex = 0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {

                    DropDownList ddlProducts =
                      (DropDownList)grdSales.Rows[rowIndex].Cells[1].FindControl("ddlProducts");

                    TextBox txtCostPrice = (TextBox)grdSales.Rows[rowIndex].Cells[2].FindControl("txtCp");
                    TextBox txtSalePrice = (TextBox)grdSales.Rows[rowIndex].Cells[3].FindControl("txtSp");
                    TextBox txtUnits = (TextBox)grdSales.Rows[rowIndex].Cells[4].FindControl("txtUnits");
                    TextBox txtTotal = (TextBox)grdSales.Rows[rowIndex].Cells[5].FindControl("txtTotal");


                    //Added these lines

                    ddlProducts.DataValueField = "Key";
                    ddlProducts.DataTextField = "Value";
                    ddlProducts.DataSource = BindProductsDdl();
                    ddlProducts.DataBind();

                    //****************

                    ddlProducts.SelectedValue = dt.Rows[i]["Products"].ToString();

                    txtCostPrice.Text = dt.Rows[i]["CostPrice"].ToString();
                    txtSalePrice.Text = dt.Rows[i]["SalePrice"].ToString();
                    txtUnits.Text = dt.Rows[i]["TotalUnits"].ToString();
                    txtTotal.Text = dt.Rows[i]["TotalCost"].ToString();
                    rowIndex++;
                }
            }
        }
    }
    private void SetRowData()
    {
        int rowIndex = 0;

        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {

                    DropDownList ddlProducts =
                       (DropDownList)grdSales.Rows[rowIndex].Cells[1].FindControl("ddlProducts");

                    TextBox txtCostPrice = (TextBox)grdSales.Rows[rowIndex].Cells[2].FindControl("txtCp");
                    TextBox txtSalePrice = (TextBox)grdSales.Rows[rowIndex].Cells[3].FindControl("txtSp");
                    TextBox txtUnits = (TextBox)grdSales.Rows[rowIndex].Cells[4].FindControl("txtUnits");
                    TextBox txtTotal = (TextBox)grdSales.Rows[rowIndex].Cells[5].FindControl("txtTotal");

                    drCurrentRow = dtCurrentTable.NewRow();

                    drCurrentRow["RowNumber"] = i + 1;

                    dtCurrentTable.Rows[i - 1]["Products"] = ddlProducts.Text;

                    dtCurrentTable.Rows[i - 1]["CostPrice"] = txtCostPrice.Text;
                    dtCurrentTable.Rows[i - 1]["SalePrice"] = txtSalePrice.Text;
                    dtCurrentTable.Rows[i - 1]["TotalUnits"] = txtUnits.Text;
                    dtCurrentTable.Rows[i - 1]["TotalCost"] = txtTotal.Text;

                    rowIndex++;
                }

                ViewState["CurrentTable"] = dtCurrentTable;
                //grvStudentDetails.DataSource = dtCurrentTable;
                //grvStudentDetails.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }
        //SetPreviousData();
    }
    private void BindControls()
    {
        BindWarehouseDdl(); BindStaticLists();

    }

    private Dictionary<int, string> BindProductsDdl()
    {
        Dictionary<int, string> Get = new Dictionary<int, string>();
        for (int i = 0; i < Products.Count; i++)
        {
            int id = Convert.ToInt32(Products[i].id);
            string name = Products[i].Name;
            Get.Add(id, name);
        }
        return Get;
    }
    private void BindStaticLists()
    {
        Classes.CProducts cp = new Classes.CProducts();
        Products = cp.GetAll();
    }
    private void BindWarehouseDdl()
    {
        Dictionary<int, string> Items = new Dictionary<int, string>();
        List<Models.MwareHouse> get = new List<Models.MwareHouse>();
        Classes.CWareHouse cw = new Classes.CWareHouse();
        get = cw.GetAll();
        for (int i = 0; i < get.Count; i++)
        {
            string id = get[i].id;
            string name = get[i].Name;
            Items.Add(Convert.ToInt32(id), name);

        }
        ddlWareHouse.DataTextField = "Value";
        ddlWareHouse.DataValueField = "Key";
        ddlWareHouse.DataSource = Items;
        ddlWareHouse.DataBind();
    }
    #endregion

    protected void ddlProducts_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void grdSales_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        GridViewRow gr = grdSales.SelectedRow;

    }

解決了它

protected void ddlProducts_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;
        GridViewRow row = (GridViewRow)ddl.NamingContainer;
        TextBox txtName = (TextBox)row.FindControl("txtCp");
        txtName.Text = "*****";

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM