使用母版页中的按钮

时间:2016-10-05 10:28:14

标签: c# asp.net

我正在创建一个Web应用程序,其中我有一个包含5个其他页面的母版页

welcome

user

employee

admin

company

每个页面都有一个gridview(i.e I have 5 gridviews)gridview包含不同页面的不同数据

我使用它将gridview数据导出到excel

protected void imgexcel_Click(object sender, ImageClickEventArgs e)
{
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-excel";

    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);

    GridView1.AllowPaging = false;
    GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");

    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
        GridViewRow row = GridView1.Rows[i];
        row.BackColor = System.Drawing.Color.White;
        row.Attributes.Add("class", "textmode");
    }

    GridView1.RenderControl(hw);

    string style = @"<style> .textmode { mso-number-format:\@; } </style>";

    Response.Write(style);
    Response.Output.Write(sw.ToString());
    Response.Flush();
    Response.End();
}
  

这是我正常的导出编码

但我想在我的母版页上添加按钮,并且数据的导出取决于用户使用的页面(如果用户在欢迎页面上,欢迎页面的数据必须在按钮点击时导出,如果他在用户页面上并单击在按钮上单击用户的数据必须导出等等)

2 个答案:

答案 0 :(得分:0)

string s = this.Page.Request.FilePath; // "/Welcome.aspx"

您可以使用此代码将页面名称保存在字符串中,这样您就可以在switch-case结构中使用此字符串(在按钮OnClick中)。

switch(string)
{
    case:"welcome": //...
    case:"user": //...
    case:"employee": //...
}

希望它有所帮助。

答案 1 :(得分:0)

您可以使用FindControl在每个页面上查找GridView。您只需确保它们都具有相同的ID。

    protected void Button1_Click(object sender, EventArgs e)
    {
        //first find the ContentPlaceHolder control
        ContentPlaceHolder contentPlaceHolder = FindControl("ContentPlaceHolder1") as ContentPlaceHolder;

        //then find the GridView control inside the ContentPlaceHolder
        GridView gridview = contentPlaceHolder.FindControl("GridView1") as GridView;

        //or the same as above but in a single line of code
        GridView gridview = FindControl("ContentPlaceHolder1").FindControl("GridView1") as GridView;

        //do stuff with the GridView
        gridview.Visible = false;
    }