I am Creating a web app in which i have a master page with 5 other page
welcome
user
employee
admin
company
Every page have a gridview ( ie I have 5 gridviews
) gridview contain different data for different page
and i used this to export the gridview data into 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();
}
this is my normal coding for export
but i want to add button on my masterpage and exporting of data depends on which page is user using (if the user is on welcome page data of welcome page must be exported on button click and if he is on user page and click on button click the data of user must be exported and so on)
string s = this.Page.Request.FilePath; // "/Welcome.aspx"
You can use this code for holding the page name in a string so you can use this string in a switch-case structure (in button OnClick
).
switch(string)
{
case:"welcome": //...
case:"user": //...
case:"employee": //...
}
Hope it helps.
You can use FindControl
to find the GridView on every page. You just have to make sure they all have the same 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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.