开发者

Selected value on a DropDownList inside of a repeater

开发者 https://www.devze.com 2023-03-06 15:06 出处:网络
I\'m adding a dropdwonlist inside of my repeater. Now I need to set a selected value for my dropdownlist.. but I\'m not being very successful..

I'm adding a dropdwonlist inside of my repeater. Now I need to set a selected value for my dropdownlist.. but I'm not being very successful..

private void Phys开发者_运维百科icianSource()
{
    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>

nothing happens on my codes.. help me guys I'm just a beginner on this.. thank you very much..


Hi Ian Ace its better to modify your code a little bit

 <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>

Now the code behind should be

  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;
}

hope the above example helps.


  1. Try setting the selected value after data binding
  2. Don't set the list contents with every postback. Check for Page.IsPostBack(). Note that this only works if you have ViewState enabled.


//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>


The problem occurs because the binding of dropdown occurs after that of repeater control. You can use HTML5 custom attributes, to set your dropdown value into a custom attribute, and then setting it as selected value after the dropdown is databinded. I have binded the dropdown using 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>

CodeBehind

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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消