谢谢
解决方案 »
- access数据库中设置的日期格式的问题
- C#和Jquery实现FusionChart的使用
- 一个初学者刚学asp.net遇到几个小问题,尝试过还是不明,请教!
- 把新浪网打开看体育新闻的时候,不是在左上角有那样的几个图片在播放,几个最新的体育图片吗?那效果是怎么样搞出来的?
- 晕了~~求助!类库无法生成DLL文件
- 请问在asp.net下如何得到一个XML字符串中指定节点的值?
- 急呼!!!Infragistics的WEB控件WebGrid拖放到WebPage中,运行报错!如下
- TreeView动态生成问题,大家帮忙看看,谢谢了
- 使用DataGrid编辑数据时的格式问题
- Update 急急急
- [VB]ASPX中session的时间怎么设定,我用session.timeout=40控制时间好像不管用啊?
- 在一个页面中使用了iframe的问题!
<ASP:gridview...>
<columns>
<asp:TemplateColumn ItemStyle-Width="70" HeaderStyle-Width="70" HeaderText="xxx">
<ItemTemplate>
<asp:DropDownList ID="aaa" runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>类似这样
SELECT mc FROM lottery_NBA WHERE (dm = ?)
在定义参数时,dm=GridView1.SelectedValue
但是执行时报错啊
*****************************************************************************
菜鸟一只
GridView的sql语句是别的
在模版列中定义参数时,dm=GridView1.SelectedValue
但是执行时报错啊
在网上看一些列子,都没有看到定义参数不知道为什么
首先是页面部分的代码
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />
<asp:BoundField DataField="CategoryName" HeaderText="Category Name" />
<asp:TemplateField HeaderText="Products">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
在codebehind部分,
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// This is because Table[1] contains Categories
GridView1.DataSource = GetDataSet().Tables[1];
GridView1.DataBind();
} }
private DataSet GetDataSet()
{
string query = @"SELECT p.CategoryID,p.ProductID, p.ProductName FROM Products p SELECT c.CategoryID,c.CategoryName FROM Categories c";
string connectionString = "Server=localhost;Database=Northwind;user id=sa;password=123456";
SqlConnection myConnection = new SqlConnection(connectionString);
SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);
DataSet ds = new DataSet();
ad.Fill(ds);
return ds;
}
在上面的代码中,首先我们通过典型的dataset返回,绑定到gridview上去,注意这里sql语句里有两句,第一句是返回产品,第二句是返回所有的类别category,而在绑定gridview时,我们用
GridView1.DataSource = GetDataSet().Tables[1];,将第一个table表里的category记录绑定到gridview里去,接下来,我们要处理模版列中的dropdownlist了,这个可以在row_databound事件中写入代码,如下
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataTable myTable = new DataTable();
DataColumn productIDColumn = new DataColumn("ProductID");
DataColumn productNameColumn = new DataColumn("ProductName");
myTable.Columns.Add(productIDColumn);
myTable.Columns.Add(productNameColumn);
DataSet ds = new DataSet();
ds = GetDataSet();
int categoryID = 0;
string expression = String.Empty;
if (e.Row.RowType == DataControlRowType.DataRow)
{
categoryID = Int32.Parse(e.Row.Cells[0].Text);
expression = "CategoryID = " + categoryID;
DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
DataRow[] rows = ds.Tables[0].Select(expression);
foreach (DataRow row in rows)
{
DataRow newRow = myTable.NewRow();
newRow["ProductID"] = row["ProductID"];
newRow["ProductName"] = row["ProductName"];
myTable.Rows.Add(newRow);
}
ddl.DataSource = myTable;
ddl.DataTextField = "ProductName";
ddl.DataValueField = "ProductID";
ddl.DataBind();
}
}
这里的原理大致如下:
首先,我们建立了一个datatable,包含了productid,productname列,然后将其与 gridview绑定,之后再用
categoryID = Int32.Parse(e.Row.Cells[0].Text);
expression = "CategoryID = " + categoryID;
构造一个筛选表达式,这里指定为categoryID,然后通过
DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
DataRow[] rows = ds.Tables[0].Select(expression);
找出符合其类别等于某一categoryID的所有产品,这里构造出datarow集合了,最后,使用循环,将某类别下的产品全部添加到datatable中去,最后将datatable和dropdownlist绑定,就实现功能了
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="mc" DataValueField="dm" SelectedValue='<%# Bind("fjxx") %>'>
</asp:DropDownList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT dm, mc FROM lottery_NBA WHERE (dm = ?)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" DefaultValue="" Name="dm" PropertyName="SelectedValue"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("fjxx") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>不知道错在哪里