求教:在gridview的行中绑定DropDownList,DropDownList的数据源函数中怎么传参数?
比如,行模板:
<asp:DropDownList ID="gv_DDL_defaultSelected" runat="server" SelectedValue='<%# Eval("defaultSelected") %>' DataSource='<%# defaultSelectedDDLDS(
id)%>' DataValueField="id" DataTextField="optionText"></asp:DropDownList>
这里的Id,就是本行数据的Id值,怎么把这个值传给后台绑定数据源的参数中?
后台C#
public SqlDataReader defaultSelectedDDLDS(string Id)
{
DAL.dbFunctions db = new DAL.dbFunctions();
Int32 id = Convert.ToInt32(Id);
return db.returnReader("select * from [yy8_systemSelectorOptions] where selectorId=" + Id, null);
}
------解决方案--------------------建议你用隐藏控件Session保存值,然后defaultSelectedDDLDS不需要传参,直接使用隐藏控件或session的值
------解决方案--------------------GridView中加DataKeyNames="ID"
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("ddl的id");
DAL.dbFunctions db = new DAL.dbFunctions();
Int32 id = Convert.ToInt32(e.Row.RowIndex);
return db.returnReader("select * from [yy8_systemSelectorOptions] where selectorId=" + id, null);
ddl.DataSource = ;
ddl.DataValueField = ;
ddl.DataTextField = ;
ddl.DataBind();
}
}
------解决方案--------------------把函数的实现写到DropDownList的selectchange事件中,换个思路试试。