locked
Values get cleared of Category column in Gridview RRS feed

  • Question

  • User-1499457942 posted

    Hi

      When i click on btnEdit value of Category Column in all rows get cleared

    protected void gvw_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                if (e.CommandName == "EditRow")
                {
                    GridViewRow row = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
                                    txt_Location.Text = ((Label)row.FindControl("lblLocation")).Text;
                    ddl_Category.SelectedValue = ((Label)row.FindControl("lblCategory")).Text;
    
                    ClientScript.RegisterStartupScript(this.GetType(), "Pop", "openModal();", true);
                }
            }
    
            protected void gvw_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    string m_Category = string.Format("{0}", DataBinder.Eval(e.Row.DataItem, "Category"));
                    using (SqlConnection con = new SqlConnection(CommonFunction.connectionString))
                    {
                        SqlCommand cmd = new SqlCommand("Select Description from [StoreCategories] where EntryNo = @Category", con);
                        cmd.Parameters.AddWithValue("@Category", m_Category);
                        cmd.CommandType = CommandType.Text;
                        if (con.State == ConnectionState.Closed)
                        {
                            con.Open();
                        }
                        e.Row.Cells[7].Text = (string)cmd.ExecuteScalar();
                    }
                }
            }
    
    
    
    <div class="row">  
            <div class="col-lg-12 "> 
                <div class="table-responsive">
                    <asp:GridView ID="gvw1" OnPreRender="gv1_PreRender" runat="server"
                        CssClass="table table-bordered table-striped" AutoGenerateColumns="false" 
                        ClientIDMode="Static" BorderWidth="1px"  OnRowCommand="gvw1_RowCommand" OnRowDataBound ="gvw1_RowDataBound"
                        BorderStyle="None" BorderColor="#DEBA84" HeaderStyle-CssClass="GridHeader" 
                        EmptyDataText="No Records Found!" EmptyDataRowStyle-ForeColor="Red" EmptyDataRowStyle-CssClass ="gvEmpty" >
                        <HeaderStyle ForeColor="White" Font-Bold="True" BackColor="#428bca"></HeaderStyle>
                        <Columns>
                            <asp:TemplateField HeaderText="Location">
                                <ItemTemplate>
                                    <asp:HiddenField ID="hdfNo" runat="server" Value='<%# Bind("No") %>' />
                                    <asp:Label runat="server" ID="lblLocation" Text='<%#Eval("Location") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            
                            <asp:TemplateField HeaderText="Category">
                                <ItemTemplate>
                                    <asp:Label runat="server" ID="lblCategory" Text='<%#Eval("Category") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            
    
                            <asp:TemplateField HeaderText="Action">
                                <ItemTemplate>
                                    <asp:linkbutton id="btnEdit"  ToolTip="Update Record" CommandName="EditRow" runat="server" CssClass="clslnkbutton"/>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                </div>
            </div>

    Thanks

    Wednesday, August 22, 2018 6:35 AM

All replies

  • User-893317190 posted

    Hi JagjitSingh ,

    I have tried your code ,  and I get the value  of the dataitem and retrieve data from database using this value. Finally I set the text of one cell using the data returned by the database in the gridview'd on rowdataBound event , but when  I click the btn edit , my returned value doesn't be cleared.

    Below is my code.

            <asp:GridView ID="gvwCategories"  runat="server"
                    CssClass="table table-bordered table-striped" AutoGenerateColumns="false" OnRowCommand="gvw_RowCommand"
                    ClientIDMode="Static" BorderWidth="1px"  OnRowDataBound="gvw_RowDataBound"
                    BorderStyle="None" BorderColor="#DEBA84" HeaderStyle-CssClass="GridHeader" 
                     >
                    <HeaderStyle ForeColor="White" Font-Bold="True" BackColor="#428bca"></HeaderStyle>
                    <Columns>
                        
                       <asp:TemplateField HeaderText="Code">
                            <ItemTemplate>
                                <asp:Label runat="server" ID="lblCode" Text='<%#Eval("Code") %>'></asp:Label>
                                <asp:HiddenField ID="hdfEntryNo" runat="server" Value='<%# Bind("EntryNo") %>' />
                         
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Description">
                            <ItemTemplate>
                                <asp:Label runat="server" ID="lblDescription" Text='<%#Eval("Description") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
    
                        <asp:TemplateField HeaderText="Category">
                            <ItemTemplate>
                               
                            </ItemTemplate>
                        </asp:TemplateField>
                        
                        <asp:TemplateField HeaderText="Action">
                            <ItemTemplate>
                                <asp:linkbutton id="Linkbutton1" Text="Edit"  ToolTip="Update Record" CommandName="EditRow" runat="server" CssClass="btn btn-primary"/> 
                            </ItemTemplate>
                        </asp:TemplateField>
    
                    </Columns>
                    </asp:GridView>

    Code behind.

      protected void gvw_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                if (e.CommandName == "EditRow")
                {
                    GridViewRow row = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
                    HiddenField hdf = (row.FindControl("hdfEntryNo") as HiddenField);
                    hdf_EntryNo.Value = hdf.Value;
    
                    txtCode.Text = (row.Cells[1].FindControl("lblCode") as Label).Text;
                    txtDescription.Text = (row.Cells[1].FindControl("lblDescription") as Label).Text;
                    ClientScript.RegisterStartupScript(this.GetType(), "Pop", "openModal();", true);
                }
            }
    
            protected void gvw_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    string m_Category = string.Format("{0}", DataBinder.Eval(e.Row.DataItem, "Categoryid"));
                    using (SqlConnection con = new SqlConnection(constr))
                    {
                        SqlCommand cmd = new SqlCommand("Select category from [Category] where id = @Category", con);
                        cmd.Parameters.AddWithValue("@Category", m_Category);
                        cmd.CommandType = CommandType.Text;
                        if (con.State == ConnectionState.Closed)
                        {
                            con.Open();
                        }
                        e.Row.Cells[2].Text = (string)cmd.ExecuteScalar();
                    }
                    
                }
            }

    How do you bind your gridview's data ? Or do you use other plugins?

    I find your code  e.Row.Cells[7].Text = (string)cmd.ExecuteScalar(); , but I don't find your gridview has 8 cells .

    Another thing, your gridview's  OnRowCommand="gvw1_RowCommand" while  your code behind only has a method named gvw_RowCommand, it seems they are not related.

    If possible, could you post  more of your code that you think related to the problem?

    Best regards,

    Ackerly Xu

     

    Thursday, August 23, 2018 3:29 AM
  • User-1499457942 posted

    Hi

    protected void gvw_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    string m_Category = string.Format("{0}", DataBinder.Eval(e.Row.DataItem, "Category"));
                    using (SqlConnection con = new SqlConnection(CommonFunction.connectionString))
                    {
                        SqlCommand cmd = new SqlCommand("Select Description from [Categories] where EntryNo = @Category", con);
                        cmd.Parameters.AddWithValue("@Category", m_Category);
                        cmd.CommandType = CommandType.Text;
                        if (con.State == ConnectionState.Closed)
                        {
                            con.Open();
                        }
                        e.Row.Cells[7].Text = (string)cmd.ExecuteScalar();
                    }
                }
            }
    
    protected void gvw_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                if (e.CommandName == "EditRow")
                {
                    GridViewRow row = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
                    HiddenField hdf_Id0 = (row.FindControl("hdfEntryNo") as HiddenField);
                    hdfId.Value = hdf_Id0.Value;
    
                    ddl_StoreCategory.SelectedValue = ((Label)row.FindControl("lblCategory")).Text;
                    ddl_Months.SelectedValue = ((Label)row.FindControl("lblMonths")).Text;
                }
            }
    
    
    
    <div class="row">  
            <div class="col-lg-12 "> 
                <div class="table-responsive">
                    <asp:GridView ID="gvw" OnPreRender="gvw_PreRender" runat="server"
                        CssClass="table table-bordered table-striped" AutoGenerateColumns="false" 
                        ClientIDMode="Static" BorderWidth="1px"   OnRowDataBound ="gvw_RowDataBound"
                        EmptyDataText="No Records Found!" EmptyDataRowStyle-ForeColor="Red" EmptyDataRowStyle-CssClass ="gvEmpty" >
                        <HeaderStyle ForeColor="White" Font-Bold="True" BackColor="#428bca"></HeaderStyle>
                        <Columns>
                            <asp:TemplateField HeaderText="Location">
                                <ItemTemplate>
                                    <asp:HiddenField ID="hdfEntryNo" runat="server" Value='<%# Bind("EntryNo") %>' />
                                    <asp:Label runat="server" ID="lblLocation" Text='<%#Eval("Location") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="Target1" HeaderText="Target1"/>
                            <asp:BoundField DataField="Achievement1" HeaderText="Achievement1" />
                            <asp:BoundField DataField="PercentAchieved1" HeaderText="Achieved1" />
                            <asp:BoundField DataField="Target2" HeaderText="Target2" />
                            <asp:BoundField DataField="Achievement2" HeaderText="Achievement2" />
                            <asp:BoundField DataField="Achieved2" HeaderText="Achieved2"  />
                            <asp:TemplateField HeaderText="Store Category">
                                <ItemTemplate>
                                    <asp:Label runat="server" ID="lblCategory" Text='<%#Eval("Category") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Months">
                                <ItemTemplate>
                                    <asp:Label runat="server" ID="lblMonths" Text='<%#Eval("Months") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Inf">
                                <ItemTemplate>
                                    <asp:Label ID="lblInf" runat="server" Text='<%# Eval("Inf") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="Infl1" HeaderText="Infl1"  />
    
                            <asp:TemplateField HeaderText="Action">
                                <ItemTemplate>
                                    <asp:linkbutton id="btnEdit"  ToolTip="Update Record" CommandName="EditRow" runat="server" CssClass="clslnkbutton"/>
                                    <asp:linkbutton id="lnkBtnDelete"  ToolTip="Delete Record"  runat="server" 
                                    CssClass="clslnkDelbutton" OnClientClick="return confirm('Are you sure you want to delete this item?');" OnClick="lnkBtnDelete_Click" /> 
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                </div>
            </div>
        </div>

    Thanks

    Thursday, August 23, 2018 10:43 AM
  • User-893317190 posted

    Hi JagjitSingh ,

    This is caused by your code

        e.Row.Cells[7].Text = (string)cmd.ExecuteScalar();

    This code will cover your category . If you use F12 developer tool , you will see  spans with empty content in your category column, because the web form starts a new lifecycle.

    You should change your code to save the category when you page posts back. There are several ways . For example , create a new column to save the data and make it invisible, use a hidden field to save the data. I try to save the data in the label's attribute. You could get the attribute through the label's attributes property.

    Below is my code. In my code , categoryId  refers to your category.

    <asp:GridView ID="gvwCategories"  runat="server"
                     CssClass="table table-bordered table-striped" AutoGenerateColumns="false" 
                        ClientIDMode="Static" BorderWidth="1px"  OnRowCommand="gvw_RowCommand" OnRowDataBound ="gvw_RowDataBound"
                        BorderStyle="None" BorderColor="#DEBA84" HeaderStyle-CssClass="GridHeader" 
                        EmptyDataText="No Records Found!" EmptyDataRowStyle-ForeColor="Red" EmptyDataRowStyle-CssClass ="gvEmpty" 
                     >
                    <HeaderStyle ForeColor="White" Font-Bold="True" BackColor="#428bca"></HeaderStyle>
                    <Columns>
                        
                       <asp:TemplateField HeaderText="Code">
                            <ItemTemplate>
                                <asp:Label runat="server" ID="lblCode" Text='<%#Eval("Code") %>'></asp:Label>
                                <asp:HiddenField ID="hdfEntryNo" runat="server" Value='<%# Bind("EntryNo") %>' />
                         
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Description">
                            <ItemTemplate>
                                <asp:Label runat="server" ID="lblDescription" Text='<%#Eval("Description") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
    
                        <asp:TemplateField HeaderText="Category">
                            <ItemTemplate>                                     <%-- here to save the data of your category --%>
                                <asp:Label runat="server" ID="lblCategory" categoryId='<%# Eval("Categoryid") %>' Text=''></asp:Label>
    
                            </ItemTemplate>
                        </asp:TemplateField>
                        
                        <asp:TemplateField HeaderText="Action">
                            <ItemTemplate>
                                <asp:linkbutton id="Linkbutton1" Text="Edit"  ToolTip="Update Record" CommandName="EditRow" runat="server" CssClass="btn btn-primary"/> 
                            </ItemTemplate>
                        </asp:TemplateField>
    
                    </Columns>
                    </asp:GridView>

     Code behind.

    protected void gvw_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                if (e.CommandName == "EditRow")
                {
                    GridViewRow row = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
                    HiddenField hdf = (row.FindControl("hdfEntryNo") as HiddenField);
                    hdf_EntryNo.Value = hdf.Value;
    
                    txtCode.Text = (row.Cells[1].FindControl("lblCode") as Label).Text;
    //get the data through the label's attributes categoryId, you could use your own name string categoryId = (row.FindControl("lblCategory") as Label).Attributes["categoryId"]; txtDescription.Text = (row.Cells[1].FindControl("lblDescription") as Label).Text; ClientScript.RegisterStartupScript(this.GetType(), "Pop", "openModal();", true); } } protected void gvw_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string m_Category = string.Format("{0}", DataBinder.Eval(e.Row.DataItem, "Categoryid")); using (SqlConnection con = new SqlConnection(constr)) { SqlCommand cmd = new SqlCommand("Select category from [Category] where id = @Category", con); cmd.Parameters.AddWithValue("@Category", m_Category); cmd.CommandType = CommandType.Text; if (con.State == ConnectionState.Closed) { con.Open(); } // show your data through the label's text property (e.Row.FindControl("lblCategory") as Label).Text = (string)cmd.ExecuteScalar(); } } }

    The result.

    Best regards,

    Ackerly Xu

    Friday, August 24, 2018 1:46 AM