我有一个搜索引擎,在伙伴关系中使用<代码>SqlDataSource控制。 信息网从具有参数的储存程序中获取数据。 我从默认价值和网页控制中得到参数,这样用户就可以获得他想要的东西。 然后在<代码>GridView控制中显示这一数据。
现在,我必须增加一个出口县,它将在一份供用户下载的Excel文件中输出结果。 I m并不确定它是否是最佳方法,但我使用了微软体的算法()。 当我试图获得搜索场的价值时,问题就会出现。 你们看到,我们把这个模板作为模板,因为我们有大约十几个搜索引擎,我们希望有能动发挥作用的东西。
Here s an exemple of the SqlDataSource
control on the aspx page :
<asp:SqlDataSource ID="SearchDataSource" runat="server"
ConnectionString="CONNECTIONSTRING"
ProviderName="System.Data.SqlClient"
SelectCommand="sp_SearchEngine_BASE"
SelectCommandType="StoredProcedure" onselected="SearchDataSource_Selected">
<SelectParameters>
<asp:ControlParameter ControlID="ctlID1" DbType="SomeType" DefaultValue=""
Name="spParamA" PropertyName="aProperty" />
<asp:ControlParameter ControlID="ctlID2" DbType="SomeType" DefaultValue=""
Name="spParamB" PropertyName="aProperty" />
</SelectParameters>
</asp:SqlDataSource>
这里还有出口法:
protected void exportExcel()
{
int i;
String strLine = "", filePath, fileName, fileExcel;
FileStream objFileStream;
StreamWriter objStreamWriter;
Random nRandom = new Random(DateTime.Now.Millisecond);
//Dim fs As Object, myFile As Object
SqlConnection cnn = new SqlConnection(SearchDataSource.ConnectionString);
//Create a pseudo-random file name.
fileExcel = "t" + nRandom.Next().ToString() + ".xls";
//Set a virtual folder to save the file.
//Make sure that you change the application name to match your folder.
filePath = Server.MapPath("\ExcelTest");
fileName = filePath + "\" + fileExcel;
//Use FileStream to create the .xls file.
objFileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
objStreamWriter = new StreamWriter(objFileStream);
//Use a DataReader to connect to the Pubs database.
cnn.Open();
String sql = SearchDataSource.SelectCommand;
SqlCommand cmd = new SqlCommand(sql, cnn);
//cmd.Parameters.Add(SearchDataSource.SelectParameters[0].);
for (i = 0; i <= SearchDataSource.SelectParameters.Count - 1; i++)
{
// ---------------------------------------
// ----- Here is where I am stuck... -----
// ---------------------------------------
//cmd.Parameters.AddWithValue(SearchDataSource.SelectParameters[i].Name, WhatDoIPutHere);
}
SqlDataReader dr;
dr = cmd.ExecuteReader();
//Enumerate the field names and records that are used to build the file.
for(i = 0; i <= dr.FieldCount - 1; i++) {
strLine = strLine + dr.GetName(i).ToString() + " ";
}
//Write the field name information to file.
objStreamWriter.WriteLine(strLine);
//Reinitialize the string for data.
strLine = "";
//Enumerate the database that is used to populate the file.
while (dr.Read()) {
for(i = 0; i<= dr.FieldCount - 1; i++) {
strLine = strLine + dr.GetValue(i) + " ";
}
objStreamWriter.WriteLine(strLine);
strLine = "";
}
//Clean up.
dr.Close();
cnn.Close();
objStreamWriter.Close();
objFileStream.Close();
/*
//Show a link to the Excel file.
HyperLink1.Text = "Open Excel";
HyperLink1.NavigateUrl = fileExcel;
*/
}
由于出口代码需要动态,因此需要在
因此,这里的问题是:How ,我可以获得<代码>所用控制值。 缩略语 SqlDataReader dr in the C#user-下限方法 Without exportExcel (
associated with my aspx page without硬编码 the control IDs?