我在我的中继器中添加了一个dropdwonlist。现在我需要为我的下拉列表设置一个选定的值..但我不是很成功..
private void PhysicianSource()
{
DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add("Source");
dt.Columns.Add("ID", Type.GetType("System.String"));
dt.Columns.Add("Desc", Type.GetType("System.String"));
Provider oProvider = new Provider();
List<ProviderLabel> lstprovider = oProvider.RetrievePhysicianList();
foreach (ProviderLabel item in lstprovider)
{
DataRow dr = dt.NewRow();
dr[0] = item.ProviderCode.ID.ToString();
dr[1] = item.Name.ToString();
dt.Rows.Add(dr);
}
drpPhysicianCode.DataSource = ds;
drpPhysicianCode.DataMember = "Source";
drpPhysicianCode.DataTextField = "ID";
drpPhysicianCode.DataValueField = "ID";
drpPhysicianCode.DataBind();
}
asp.net
<asp:Repeater ID="rptrPatientList" runat="server" >
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<tr>
<td class="style2">
<asp:DropDownList ID="DropDownList1"
DataTextField="ID" runat="server"
DataValueField="Desc"
SelectedValue='<%# Eval("Code") %>'
DataSource="ds">
</asp:DropDownList>
</td>
</td>
</tr>
我的代码没有任何反应..帮助我们,我只是一个初学者..非常感谢..
答案 0 :(得分:1)
嗨Ian Ace更好地修改你的代码
<asp:Repeater ID="rptProductList" runat="server" OnItemDataBound="rptProductList_ItemDataBound">
<ItemTemplate>
<asp:DropDownList runat="server" ID="MyRepeater" AutoPostBack="true" OnSelectedIndexChanged="DropDownList_SelectedIndexChanged" >
</asp:DropDownList>
</ItemTemplate>
</asp:Repeater>
现在背后的代码应该是
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
fillRepeater();
}
}
}
protected void rptProductList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DropDownList MyDropDown= (DropDownList)e.Item.FindControl("MyRepeater");
if (MyDropDown!= null)
{
MyDropDown.DataSource = fillDropDown(MyDropDown);
MyDropDown.DataBind();
}
}
}
private DataSet fillDropDown(DropDownList dropDown)
{
// get your collection and return.
}
protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList MyDropDown= (DropDownList)sender;
string item = MyDropDown.SelectedValue;
}
希望上面的例子有所帮助。
答案 1 :(得分:0)
答案 2 :(得分:0)
//Declare Class Scoped DataSet and DataTable variables
DataSet ds;
DataTable dt;
//Bind repeater and DataSource method
private void PhysicianSource()
{
ds = new DataSet();
dt = ds.Tables.Add("Source");
dt.Columns.Add("ID", Type.GetType("System.String"));
dt.Columns.Add("Desc", Type.GetType("System.String"));
Provider oProvider = new Provider();
List<ProviderLabel> lstprovider = oProvider.RetrievePhysicianList();
foreach (ProviderLabel item in lstprovider)
{
DataRow dr = dt.NewRow();
dr[0] = item.ProviderCode.ID.ToString();
dr[1] = item.Name.ToString();
dt.Rows.Add(dr);
}
drpPhysicianCode.DataSource = ds;
drpPhysicianCode.DataMember = "Source";
drpPhysicianCode.DataTextField = "ID";
drpPhysicianCode.DataValueField = "ID";
drpPhysicianCode.DataBind();
}
//Repeater Item Data Bound event in which we would find Controls to be bound
//and set DataSource and SelectedValue
protected void rptrPatientList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DropDownList DropDownList1 = e.Item.FindControl("DropDownList1") as DropDownList;
if (DropDownList1 != null)
{
DropDownList1.DataSource = dt;
DropDownList1.DataBind();
DropDownList1.SelectedValue = // DataBinder Eval 'Code' //;
}
}
}
<asp:Repeater ID="rptrPatientList" runat="server" OnItemDataBound="rptrPatientList_ItemDataBound">
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<tr>
<td class="style2">
<asp:DropDownList ID="DropDownList1"
DataTextField="ID" runat="server"
DataValueField="Desc"
SelectedValue='<%# Eval("Code") %>'
DataSource="ds">
</asp:DropDownList>
</td>
</td>
</tr>
答案 3 :(得分:0)
出现此问题是因为在转发器控制之后发生了下拉列表的绑定。您可以使用HTML5 custom attributes将下拉列值设置为自定义属性,然后在下拉列表进行数据化后将其设置为选定值。我使用asp:ObjectDataSource
绑定了下拉列表<强> ASPX 强>
<asp:Repeater ID="rptrPatientList" runat="server" >
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<tr>
<td class="style2">
<asp:DropDownList ID="DropDownList1"
DataTextField="ID" runat="server"
DataValueField="Desc"
SetValue='<%# Eval("Code") %>' datasourceid="dsCategory"
datatextfield="ID" datavaluefield="desc" DataMember = "Source" onprerender="DropDownDataBinding">
</asp:DropDownList>
<asp:ObjectDataSource ID="dsCategory" runat="server" SelectMethod="PhysicianSource" TypeName="WebApplication.WebForm1" />
</td>
</td>
</tr>
<强>代码隐藏强>
protected void DropDownDataBinding(object sender, EventArgs e) //Method to set the selected value on Category dropdown inside repeater
{
DropDownList sel = (DropDownList)sender;
sel.Value = sel.Attributes["SetValue"];
ListItem li = new ListItem("<< Select >>", "");
sel.Items.Insert(0,li);
}
protected DataSet PhysicianSource()
{
DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add("Source");
dt.Columns.Add("ID", Type.GetType("System.String"));
dt.Columns.Add("Desc", Type.GetType("System.String"));
Provider oProvider = new Provider();
List<ProviderLabel> lstprovider = oProvider.RetrievePhysicianList();
foreach (ProviderLabel item in lstprovider)
{
DataRow dr = dt.NewRow();
dr[0] = item.ProviderCode.ID.ToString();
dr[1] = item.Name.ToString();
dt.Rows.Add(dr);
}
return ds;
}