locked
Gridview in checkbox asp net update panel RRS feed

  • Question

  • User197255166 posted

    Hello everyone, my problem is that I'm exporting the gridview to another gridview with the selected checkbox data. But if I use an update panel this does not work. How can I solve this problem.

    Code Behind

      protected void chcsec_CheckedChanged(object sender, EventArgs e)
            {
    
             
                if ((sender as CheckBox).Checked)
                {
                    if (ViewState["CheckedRow"] == null)
                    {
                        DataTable dt = CheckedData();
                        ViewState["CheckedRow"] = dt;
                    }
                    else
                    {
                        DataTable previousCheckedRows = ViewState["CheckedRow"] as DataTable;
                        DataTable dtCurrentCheckedRows = CheckedData();
                        dtCurrentCheckedRows.Merge(previousCheckedRows);
                        ViewState["CheckedRow"] = dtCurrentCheckedRows;
                    }
                }
                else
                {
                    GridViewRow row = ((sender as CheckBox).NamingContainer as GridViewRow);
                    Label lblUrun = (row.FindControl("lblUrun") as Label);
                    Label lblStok = (row.FindControl("lblStok") as Label);
    
                    DataTable deleteRow = (ViewState["CheckedRow"] as DataTable).DefaultView.ToTable(true);
                    DataRow[] dr = deleteRow.Select("UrunAd = '" + lblUrun.Text + "' AND Stok = '" + lblStok.Text + "'");
                    foreach (DataRow d in dr)
                    {
                        deleteRow.Rows.Remove(d);
                    }
                    ViewState["CheckedRow"] = deleteRow;
                }
                gvSiparis.DataSource = (ViewState["CheckedRow"] as DataTable).DefaultView.ToTable(true);
                gvSiparis.DataBind();
    
            }
    
    
            private DataTable CheckedData()
            {
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[] { new DataColumn("UrunAd"), new DataColumn("Stok", typeof(int)) });
                foreach (GridViewRow rw in example.Rows)
                {
    
                    if (rw.RowType == DataControlRowType.DataRow)
                    {
                      
                        CheckBox chk = (CheckBox)rw.Cells[0].FindControl("chcsec");
                        if (chk.Checked)
                        {
                            //HiddenField hdID = (HiddenField)rw.Cells[0].FindControl("hdID");
                            Label lb = (Label)rw.Cells[1].FindControl("lblUrun");
                            Label lb1 = (Label)rw.Cells[2].FindControl("lblStok");
                       
    
                            int stok = Convert.ToInt32(lb1.Text);                       
    
    
    
                            dt.Rows.Add(lb.Text, lb1.Text);
    
                            chk.Checked = false;
                        }
    
                    }
                }
    
                return dt;
            }

    Html Code

       <asp:UpdatePanel runat="server" UpdateMode="Conditional">
                                                                <ContentTemplate>
    
    
                                                                    <asp:GridView ID="example" CssClass="table table-striped table-bordered table-hover" AutoGenerateColumns="false" DataKeyNames="ID" runat="server" ClientIDMode="Static">
    
                                                                        <Columns>
    
                                                                            <asp:TemplateField HeaderText="Ürün Seç" ItemStyle-Width="10%">
    
                                                                                <ItemTemplate>
    
                                                                                    <asp:CheckBox ID="chcsec" CssClass="form-control" runat="server" AutoPostBack="true" OnCheckedChanged="chcsec_CheckedChanged" />
    
    
    
                                                                                </ItemTemplate>
                                                                            </asp:TemplateField>
                                                                            <%--   <asp:TemplateField HeaderText="Ürün ID" ItemStyle-Width="100">
                                                                    
                                                                        <ItemTemplate>
                                        <asp:HiddenField ID="hdID" runat="server" Text='<%# Eval("ID") %>'/>
    
                                                                            <asp:HiddenField ID="hdID" runat="server" Value='<%# Eval("ID") %>' />
                                                                          
                                                                        </ItemTemplate>
                                                                    
                                                                    </asp:TemplateField>--%>
    
    
    
                                                                            <asp:TemplateField HeaderText="Ürün Ad" ItemStyle-Width="20%">
    
                                                                                <ItemTemplate>
                                                                                    <asp:Label ID="lblUrun" runat="server" Text='<%# Eval("UrunAd") %>'></asp:Label>
                                                                                </ItemTemplate>
                                                                                <EditItemTemplate>
                                                                                    <asp:TextBox ID="txturunad" runat="server" Text='<%# Eval("UrunAd") %>'></asp:TextBox>
                                                                                </EditItemTemplate>
                                                                            </asp:TemplateField>
    
    
                                                                            <asp:TemplateField HeaderText="Stok" ItemStyle-Width="10%">
                                                                                <ItemTemplate>
                                                                                    <asp:Label ID="lblStok" Style="padding: 3px 5px;" runat="server" Text='<%# Eval("Stok") %>'></asp:Label>
                                                                                </ItemTemplate>
                                                                                <EditItemTemplate>
                                                                                    <asp:TextBox ID="txtstok" Style="padding: 3px 5px;" runat="server" Text='<%# Eval("Stok") %>'></asp:TextBox>
                                                                                </EditItemTemplate>
                                                                            </asp:TemplateField>
    
                                                                            <%--   <asp:TemplateField HeaderText="Fiyat" ItemStyle-Width="50">
                                                                        <ItemTemplate>
                                                                            <asp:TextBox ID="txtfiyat" Style="padding: 3px 5px; width: 100%" CssClass="form-control" runat="server"></asp:TextBox>
                                                                        </ItemTemplate>
                                                                        <EditItemTemplate>
                                                                            <asp:TextBox ID="txtfiyat" Style="padding: 3px 5px; width: 100%" runat="server" CssClass="form-control"></asp:TextBox>
                                                                        </EditItemTemplate>
                                                                    </asp:TemplateField>--%>
    
                                                                            <%--     <asp:TemplateField HeaderText="Adet" ItemStyle-Width="50">
                                                                        <ItemTemplate>
                                                                            <asp:TextBox ID="txtAdet" Style="padding: 3px 5px; width: 100%" CssClass="form-control" runat="server"></asp:TextBox>
                                                                        </ItemTemplate>
                                                                        <EditItemTemplate>
                                                                            <asp:TextBox ID="txtAdet" runat="server" Style="padding: 3px 5px; width: 100%" CssClass="form-control"></asp:TextBox>
                                                                        </EditItemTemplate>
                                                                    </asp:TemplateField>--%>
    
    
    
                                                                            <%--    <asp:TemplateField HeaderText="Eleman Seç" ItemStyle-Width="100">
    
                                                                        <ItemTemplate>
                                                                            <asp:DropDownList ID="drpEleman" CssClass="form-control" runat="server"></asp:DropDownList>
                                                                        </ItemTemplate>
    
    
                                                                    </asp:TemplateField>--%>
    
                                                                            <%--  <asp:TemplateField HeaderText="Eleman Adet" ItemStyle-Width="50">
                                                                        <ItemTemplate>
                                                                            <asp:TextBox ID="txtElemanAdet" Style="padding: 3px 5px; width: 100%" CssClass="form-control" runat="server"></asp:TextBox>
                                                                        </ItemTemplate>
                                                                        <EditItemTemplate>
                                                                            <asp:TextBox ID="txtElemanAdet" runat="server" Style="padding: 3px 5px; width: 100%" CssClass="form-control"></asp:TextBox>
                                                                        </EditItemTemplate>
                                                                    </asp:TemplateField>--%>
    
                                                                            <%--              <asp:TemplateField HeaderText="Eleman Fiyat" ItemStyle-Width="50">
                                                                        <ItemTemplate>
                                                                            <asp:TextBox ID="txtElemanFiyat" Style="padding: 3px 5px; width: 100%" CssClass="form-control" runat="server"></asp:TextBox>
                                                                        </ItemTemplate>
                                                                        <EditItemTemplate>
                                                                            <asp:TextBox ID="txtElemanFiyat" runat="server" Style="padding: 3px 5px; width: 100%" CssClass="form-control"></asp:TextBox>
                                                                        </EditItemTemplate>
                                                                    </asp:TemplateField>--%>
    
    
    
                                                                            <%--   <asp:CommandField ButtonType="Link" ShowEditButton="true" ShowDeleteButton="true" ItemStyle-Width="150" />--%>
                                                                        </Columns>
    
                                                                    </asp:GridView>
                                                                  
                                                                </ContentTemplate>                                
                                                            </asp:UpdatePanel>

    Thursday, December 15, 2016 9:27 PM

Answers

  • User283571144 posted

    Hi Aliyilmaz,

    Hello everyone, my problem is that I'm exporting the gridview to another gridview with the selected checkbox data. But if I use an update panel this does not work. How can I solve this problem.

    According to your code, I have written a test demo on my computer.

    I guess you put the gvSiparis girdview out of the updatepanel.

    As far as I know, updatepanel will partly refresh the page.

    If you put gvSiparis gridview out of the updatepanel, it will not be reloaded.

    I suggest you could put the gvSiparis gridview inside the updatepanel, it will work well.

    More details, you could refer to follow codes:

    Aspx:

    <div>
                <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
                <asp:UpdatePanel runat="server" UpdateMode="Conditional"  >
                    <ContentTemplate>
                        <asp:GridView ID="example" CssClass="table table-striped table-bordered table-hover" AutoGenerateColumns="false" DataKeyNames="ID" runat="server" ClientIDMode="Static">
                            <Columns>
                                <asp:TemplateField HeaderText="Ürün Seç" ItemStyle-Width="10%">
                                    <ItemTemplate>
                                        <asp:CheckBox ID="chcsec" CssClass="form-control" runat="server" AutoPostBack="true"  OnCheckedChanged="chcsec_CheckedChanged" />
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Ürün Ad" ItemStyle-Width="20%">
                                    <ItemTemplate>
                                        <asp:Label ID="lblUrun" runat="server" Text='<%# Eval("UrunAd") %>'></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txturunad" runat="server" Text='<%# Eval("UrunAd") %>'></asp:TextBox>
                                    </EditItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Stok" ItemStyle-Width="10%">
                                    <ItemTemplate>
                                        <asp:Label ID="lblStok" Style="padding: 3px 5px;" runat="server" Text='<%# Eval("Stok") %>'></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtstok" Style="padding: 3px 5px;" runat="server" Text='<%# Eval("Stok") %>'></asp:TextBox>
                                    </EditItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView> 
                       <asp:GridView ID="gvSiparis" runat="server"></asp:GridView>
                    </ContentTemplate>
                </asp:UpdatePanel>
            </div>

    Code-behind:

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable d1 = new DataTable();
                    d1.Columns.Add("ID");
                    d1.Columns.Add("UrunAd");
                    d1.Columns.Add("Stok");
                    d1.Rows.Add("1", "aaa", "1");
                    d1.Rows.Add("2", "bbb", "2");
                    d1.Rows.Add("3", "ccc", "3");
                    example.DataSource = d1;
                    example.DataBind();
                }
            }
    
            protected void chcsec_CheckedChanged(object sender, EventArgs e)
            {
                if ((sender as CheckBox).Checked)
                {
                    if (ViewState["CheckedRow"] == null)
                    {
                        DataTable dt = CheckedData();
                        ViewState["CheckedRow"] = dt;
                    }
                    else
                    {
                        DataTable previousCheckedRows = ViewState["CheckedRow"] as DataTable;
                        DataTable dtCurrentCheckedRows = CheckedData();
                        dtCurrentCheckedRows.Merge(previousCheckedRows);
                        ViewState["CheckedRow"] = dtCurrentCheckedRows;
                    }
                }
                else
                {
                    GridViewRow row = ((sender as CheckBox).NamingContainer as GridViewRow);
                    Label lblUrun = (row.FindControl("lblUrun") as Label);
                    Label lblStok = (row.FindControl("lblStok") as Label);
    
                    DataTable deleteRow = (ViewState["CheckedRow"] as DataTable).DefaultView.ToTable(true);
                    DataRow[] dr = deleteRow.Select("UrunAd = '" + lblUrun.Text + "' AND Stok = '" + lblStok.Text + "'");
                    foreach (DataRow d in dr)
                    {
                        deleteRow.Rows.Remove(d);
                    }
                    ViewState["CheckedRow"] = deleteRow;
                }
                gvSiparis.DataSource = (ViewState["CheckedRow"] as DataTable).DefaultView.ToTable(true);
                gvSiparis.DataBind();
    
            }
    
            private DataTable CheckedData()
            {
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[] { new DataColumn("UrunAd"), new DataColumn("Stok", typeof(int)) });
                foreach (GridViewRow rw in example.Rows)
                {
    
                    if (rw.RowType == DataControlRowType.DataRow)
                    {
    
                        CheckBox chk = (CheckBox)rw.Cells[0].FindControl("chcsec");
                        if (chk.Checked)
                        {
                            //HiddenField hdID = (HiddenField)rw.Cells[0].FindControl("hdID");
                            Label lb = (Label)rw.Cells[1].FindControl("lblUrun");
                            Label lb1 = (Label)rw.Cells[2].FindControl("lblStok");
                            int stok = Convert.ToInt32(lb1.Text);
                            dt.Rows.Add(lb.Text, lb1.Text);
    
                            chk.Checked = false;
                        }
    
                    }
                }
    
                return dt;
            }

    Result:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 16, 2016 1:50 AM