Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi everybody.
I had a problem with my masterPage and content page for that.
First of all,there is a masterPage in my asp.net project,that has a box for dynamic display of
news and because of this dynamicity, i have BindData for masterPage in Load event function.
Like below:
C#
protected void Page_Load(object sender, EventArgs e)
    {
        fillPage();         // a function for making some html codes of showing news
        this.DataBind();
    }
protected void fillPage()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbcon1"].ConnectionString);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "SELECT id,topic FROM news";
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;
        DataSet ds = new DataSet();
        con.Open();
        da.Fill(ds);
        con.Close();
        List<List<string>> news = new List<List<string>>();
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            List<string> temp = new List<string>();
            temp.Add(ds.Tables[0].Rows[i].ItemArray[0].ToString());
            temp.Add(ds.Tables[0].Rows[i].ItemArray[1].ToString());
            news.Add(temp);
        }
        for (int i = 0; i < news.Count; i++)
        {
            html += "<li><a href=\"news_text.aspx?newsID=" + news[i][0] + "\">" + news[i][1] + "</a></li>";
        }
    }


And i have a content page of the mentioned masterPage, that has a gridview to display the
products list. This List is being used in removing the products and it has a remove checkbox.
At the first load of page,there is no problem in displaying the grid rows.
But when i wanna remove a product, after checkmarking the checkbox and clicking the remove
button,the problem occurs. The problem is about the DataBind in the masterPage, because
i load data for the GridView if the page is not postBack, so binding of data in the master page,
causes GridView to have nothing in it.
The .aspx code for GridView is in here:
ASP.NET
<asp:GridView runat="server" ID="products_table" EnableViewState="true" AutoGenerateColumns="false" AllowPaging="true" OnPageIndexChanging="products_table_indexChanging" style="font-family:Tahoma;font-size:12px;" CellPadding="5" HorizontalAlign="Center" CssClass="gridMain" DataKeyNames="id">
                <RowStyle CssClass="gridRow" />
                <SelectedRowStyle CssClass="gridSelectedRow" />
                <HeaderStyle CssClass="gridHeader" />
                <Columns>
                    <asp:BoundField DataField="id" HeaderText="id" Visible="false" />
                    <asp:BoundField DataField="name" HeaderText="نام محصول" />
                    <asp:BoundField DataField="brand" HeaderText="مارک" />
                    <asp:BoundField DataField="cost" HeaderText="قیمت" />
                    <asp:BoundField DataField="comments" HeaderText="توضیحات" />
                    <asp:BoundField DataField="imgName" HeaderText="نام فایل عکس" />
                    <asp:TemplateField HeaderText="حذف">
                        <ItemTemplate>
                            <asp:CheckBox ID="checkRemove" ClientIDMode="Static" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>


And the .cs code for the mentioned content page's Load event that fills GridView is like this:
C#
protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbcon1"].ConnectionString);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "SELECT * FROM Products";
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;
        DataSet ds = new DataSet();
        con.Open();
        da.Fill(ds);
        con.Close();

        if (!Page.IsPostBack)
        {
            products_table.DataSource = ds;
            products_table.DataBind();
        }
    }


Thanks.
Posted

1 solution

By putting the DataBind of masterPage's load function in the condition of not being postBacked,
the problem got solved.
Like this:
C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fillPage();
            this.DataBind();
        }
    }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900