![](/img/trans.png)
[英]How to have a button in MasterPage and use differently in content pages
[英]use button from masterpage
我正在创建一个Web应用程序,其中有一个主页和其他5个页面
welcome
user
employee
admin
company
每个页面都有一个gridview( ie 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();
}
这是我正常的出口编码
但是我想在我的母版页上添加按钮,数据的导出取决于用户正在使用哪个页面(如果用户在欢迎页面上,欢迎页面的数据必须在按钮单击时导出,如果他在用户页面并单击按钮单击,则必须导出)必须导出用户的数据,依此类推)
string s = this.Page.Request.FilePath; // "/Welcome.aspx"
您可以使用此代码将页面名称保留在字符串中,以便可以在切换用例结构(在按钮OnClick
)中使用此字符串。
switch(string)
{
case:"welcome": //...
case:"user": //...
case:"employee": //...
}
希望能帮助到你。
您可以使用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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.