如何DataBind masterPage并避免内容页面的GridView变空 [英] How to DataBind the masterPage and avoid content page's GridView getting empty

查看:133
本文介绍了如何DataBind masterPage并避免内容页面的GridView变空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。

我的masterPage和内容页面有问题。

首先,我的asp.net项目中有一个masterPage,有一个用于动态显示

新闻的框,由于这种动态性,我在加载事件功能中为masterPage设置了BindData。

如下所示:

 受保护  void  Page_Load( object  sender,EventArgs e)
{
fillPage(); // 制作一些显示新闻的HTML代码的函数
.DataBind();
}
受保护 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< string>> news = new 列表< List< string>>();
for int i = 0 ; i < ds.Tables [ 0 ]。Rows.Count; i ++)
{
List< string> temp = new List< string>();
temp.Add(ds.Tables [ 0 ]。行[i] .ItemArray [ 0 ]的ToString());
temp.Add(ds.Tables [ 0 ]。行[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 ] + < /一个>< /锂>中;
}
}





我有一个提到的masterPage的内容页面,它有一个gridview来显示

产品列表。此列表用于删除产品,它有一个删除复选框。

在第一次加载页面时,显示网格行没有问题。

但是当我要删除产品时,勾选复选框并单击删除

按钮后,会出现问题。问题是关于masterPage中的DataBind,因为如果页面不是postBack,那么

i为GridView加载数据,所以在母版页中绑定数据,

会导致GridView什么都没有。

GridView的.aspx代码在这里:

  <   asp:GridView     runat   =  server    ID   =  products_table    EnableViewState   =  true    AutoGenerateColumns   = <跨度class =code-keyword> false    AllowPaging   =  true    OnPageIndexChanging   =  products_table_indexChanging    style   =  font-family:Tahoma; font-size :12px;    CellPadding   =  5    Horizo​​ntalAlign   = 中心    CssClass   =  gridMain    DataKeyNames   =  id >  
< RowStyle CssClass = gridRow / >
< SelectedRowStyle CssClass = gridSelectedRow / >
< HeaderStyle CssClass = gridHeader / >
< >
< asp:BoundField DataField = id < span class =code-attribute> HeaderText = id 可见 = false / >
< asp:BoundField DataField = name HeaderText = ناممحصول / >
< asp:BoundField DataField = 品牌 HeaderText = مارک / >
< asp:BoundField DataField = cost HeaderText = قیمت / >
< asp:BoundField DataField = < span class =code-keyword> comments HeaderText = توضیحات / >
< asp:BoundField DataField = imgName HeaderText = نامفایلعکس / & gt;
< asp:TemplateField HeaderText = حذف >
< ItemTemplate >
< asp:CheckBox ID = checkRemove ClientIDMode = 静态 鲁纳t = server / >
< / ItemTemplate >
< / asp:TemplateField >
< /列 >
< / asp:GridView >





所提到的内容页面填充GridView的Load事件的.cs代码如下:

 受保护  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();
}
}





谢谢。

解决方案

< blockquote>通过将masterPage的加载函数的DataBind置于不被postBacked的状态,

问题得到解决。

像这样:

  protected   void  Page_Load( object  sender,EventArgs e)
{
if (!IsPostBack)
{
fillPage() ;
this .DataBind();
}
}


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:

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: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:

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.

解决方案

By putting the DataBind of masterPage's load function in the condition of not being postBacked,
the problem got solved.
Like this:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fillPage();
            this.DataBind();
        }
    }


这篇关于如何DataBind masterPage并避免内容页面的GridView变空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆