我在中继器中有一个 GridView,我正在毫无问题地导出到 Excel。当我尝试使用 DropDown.SelectedValue 使用 RowCreated 事件将其放入 GridView 的标题中时,我总是得到第一项而不是 SelectedValue。有人可以帮我吗?
我有 2 个下拉列表 ddlType 和 ddlYear 并且它总是给我返回第一个项目的值:
if (e.Row.RowType == DataControlRowType.Header)
{
if (ddlType.SelectedValue != "0")
contador = int.Parse(ddlType.SelectedValue)-1;
GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
string HeaderBackColor = "#EDEDED";
rowHeader.BackColor = System.Drawing.ColorTranslator.FromHtml(HeaderBackColor);
Literal newCells = new Literal();
newCells.Text = ddlYear.SelectedValue+@"</th>" +
"</tr>" +
"<tr bgcolor='" + HeaderBackColor + "'>";
newCells.Text += @"<th colspan='8' style='width:100px' >" + typeA + "</th>" +
"</tr>" +
"<tr bgcolor='" + HeaderBackColor + "'>";
switch (Nav)
{
case "0":
newCells.Text += @"<th colspan='2' style='width:80px' >item1</th>
<th colspan='2' style='width:80px'>item2</th>
<th colspan='2' style='width:80px'>item3</th>
<th colspan='2' style='width:80px'>tem4</th>";
break;
case "1":
newCells.Text += @"<th colspan='2' style='width:80px' >item1</th>";
break;
case "2":
newCells.Text += @"<th colspan='2' style='width:80px'>item2</th>";
break;
case "3":
newCells.Text += @"<th colspan='2' style='width:80px'>item3</th>";
break;
case "4":
newCells.Text += @"<th colspan='2' style='width:80px'>item4</th>";
break;
}
newCells.Text += @"</tr><tr bgcolor='" + HeaderBackColor + "'>";
if(Nav=="0")
newCells.Text += @"<th>N</th>
<th>E</th>
<th>N</th>
<th>E</th>
<th>N</th>
<th>E</th>
<th>N</th>
<th>E";
else
newCells.Text += @"<th>N</th>
<th>E";
TableCellCollection cells = e.Row.Cells;
TableHeaderCell headerCell = new TableHeaderCell();
headerCell.RowSpan = 4;
headerCell.Controls.Add(newCells);
headerCell.BackColor = System.Drawing.ColorTranslator.FromHtml("#6495ED");
headerCell.ForeColor = System.Drawing.ColorTranslator.FromHtml("#FFF");
rowHeader.Cells.Add(headerCell);
rowHeader.Visible = true;
((GridView)sender).Controls[0].Controls.AddAt(0, rowHeader);
}
我正在使用以下方法导出到 Excel:
string attachment = "attachment; filename=Contacts.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
MyRepeater.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();