在提交按钮单击后,在下一页的标签中显示Gridview复选框

人气:438 发布:2022-09-22 标签: asp.net c# sql-server ASP.NET

问题描述

亲爱的所有人, 我正在使用ASP.NET,C#,SQLSERVER 2005. 在我的网页上我有网格视图和gridveiw中的3列,,,,在这个网格视图下面我有提交按钮...当我选择一个复选框并点击提交按钮时。 上一页复选框所选值必须以标签显示。 请帮帮我朋友 感谢永远

Dear All, am working on ASP.NET , C#, SQLSERVER 2005. on my webpage i have gridview with checkboxes and 3 columns in gridveiw,,,, below this gridview i have submit button... When I select one Checkbox and click on submit button. On Next Page Checkbox Selected Value must be displayed in label. Please help me friends Thanks for ever

推荐答案

protected void gvrecords_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            CheckBox headerchk = (CheckBox)gvrecords.HeaderRow.FindControl("chkheader");
            CheckBox childchk = (CheckBox)e.Row.FindControl("chkSelect");
            childchk.Attributes.Add("onclick", "javascript:Selectchildcheckboxes('" + headerchk.ClientID + "')");
        }
    }
    protected void UpdateSupplyLed(object sender, EventArgs e)
    {
        double total = 0;
        CheckBox chkSampleStatus = sender as CheckBox;
        bool sample = chkSampleStatus.Checked;
        GridViewRow row = chkSampleStatus.NamingContainer as GridViewRow;
        foreach (GridViewRow gvrow in gvrecords.Rows)
        {
            CheckBox chk = (CheckBox)gvrow.FindControl("chkSelect");
            CheckBox headerchk = (CheckBox)gvrecords.HeaderRow.FindControl("chkheader");
            if (headerchk != null & headerchk.Checked)
            {
                total += Convert.ToDouble(gvrecords.DataKeys[gvrow.RowIndex].Value.ToString());
 
            }
            else if (chk != null & chk.Checked)
            {
                total += Convert.ToDouble(gvrecords.DataKeys[gvrow.RowIndex].Value.ToString());

            }
        }
        lbltotal.Text = Convert.ToString( total);
    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
       
        objsupplyPL.username = Session["username"].ToString();
        objsupplyPL.indentid = Convert.ToInt64(hdnunique.Value.Trim());

        foreach (GridViewRow gvrow in gvrecords.Rows)
        {
            CheckBox chk = (CheckBox)gvrow.FindControl("chkSelect");
            if (chk != null & chk.Checked)
            {
                objsupplyPL.grno = gvrow.Cells[13].Text.ToString();               
                objsupplyBAL.InsertFeedPaymentDetailswithgrno(objsupplyPL);                
            }
        }
    
        objsupplyPL.totalamount =Convert.ToDouble( lbltotal.Text.ToString());
        objsupplyPL.paymenttype = ddlypaymenttype.SelectedItem.Text;
        objsupplyPL.bankdetails = Convert.ToInt32(ddlbankaccount.SelectedValue.ToString());
        objsupplyPL.chequenumber = txtcheque.Text.ToString() == "" ? null : txtcheque.Text.ToString();
        objsupplyPL.rtgs = txtRtgs.Text.ToString() == "" ? 0 : Convert.ToDouble(txtRtgs.Text.ToString());
        objsupplyBAL.InsertFeedPaymentDetailswithgrno1(objsupplyPL);





        grid();
        clear();

       
    }

将网格复选框的 AutoPostback 属性设置为true并创建所选index更改了复选框的事件,并将所选项存储在Session中。现在,当您转到下一页时,您可以从会话中引用所选值。查找以下代码示例,了解相同类型的功能: ASPX代码: Set AutoPostback property of your grid checkbox to true and create selected index changed event of your checkbox and store the selected item in Session. Now when you go to the next page you can refer selected value from session. Find below code sample for the same kind of functionality : ASPX Code :
<asp:GridView runat="server" ID="gridMain" AutoGenerateColumns="False" OnRowDataBound="gridMainRowDataBound">
        <Columns>
            <asp:BoundField DataField="EmployeeName" HeaderText="Name" />
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBoxList runat="server" ID="checkDivisions" AutoPostBack="True" OnSelectedIndexChanged="divisonChanged"/>                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

ASPX.cs代码:

ASPX.cs Code :

public partial class About : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BindGrid();
            }
        }

        private void BindGrid()
        {
            var empList = new List<Employee>();
            for (var i = 0; i < 5; i++)
            {
                var employee = new Employee
                                   {
                                       EmployeeName = "Name_" + i,
                                       Divisions =
                                           new List<Division>
                                               {
                                                   new Division
                                                       {
                                                           DivisionName = "A",
                                                           DivisonID = "1"
                                                       },
                                                   new Division
                                                       {
                                                           DivisionName = "B",
                                                           DivisonID = "2"
                                                       },
                                                   new Division
                                                       {
                                                           DivisionName = "C",
                                                           DivisonID = "3"
                                                       }
                                               }
                                   };

                empList.Add(employee);
            }

            this.gridMain.DataSource = empList;
            this.gridMain.DataBind();
        }

        protected void gridMainRowDataBound(object sender, GridViewRowEventArgs e)
        {
            var divisonList = e.Row.Cells[1].FindControl("checkDivisions") as CheckBoxList;

            if (divisonList != null)
            {
                divisonList.DataSource = DataBinder.Eval(e.Row.DataItem, "Divisions");
                divisonList.DataTextField = "DivisionName";
                divisonList.DataValueField = "DivisonID";
                divisonList.DataBind();
            }
        }

        protected void divisonChanged(object sender, EventArgs e)
        {
            Session["LastSelectedItem"] = ((ListControl)sender).SelectedItem.Text;
        }
    }

    /// <summary>
    /// The empl.
    /// </summary>
    public class Employee
    {
        public string EmployeeName { get; set; }

        public List<Division> Divisions { get; set; }
    }

    /// <summary>
    /// The empl 2.
    /// </summary>
    public class Division
    {
        public string DivisionName { get; set; }

        public string DivisonID { get; set; }
    }

创建一个链接按钮 in this < asp: linkbutton id =LinkBut​​ton1runat =serverpostbackurl =<%#〜=guest =default.aspx?id =+ Eval(uniqueid)=%& gt; =xmlns:asp =#unknown>添加到购物车 ////////////////////////// ///////////////////////////////////////// 然后 在下一页上你可以在页面加载上的labelid上检索这个值,就像这样 Label3.Text = Request.QueryString [id ]; 试试让我知道 create a link button in this <asp:linkbutton id="LinkButton1" runat="server" postbackurl="<%#" ~="" guest="" default.aspx?id="+Eval(" uniqueid")="" %&gt;"="" xmlns:asp="#unknown">Add To Cart /////////////////////////////////////////////////////////////////// then on next page you can retrieve this value on labelid on page load like this Label3.Text = Request.QueryString["id"]; try and let me know

832