Logo 
Search:

Asp.net Answers

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds
  Question Asked By: Zobah Mizrachi   on Dec 18 In Asp.net Category.

  
Question Answered By: Hababah Younis   on Dec 18

The problem is this ::

You have multiple (since it is muliplied by the repeater) control instances with
the same ID, i.e. your dropdowns.

It is a bit confusing because the repeater  does not suffer these problems as it
uses UniqueID for itself and its child controls.

If you don't databind the dropdowns it will work fine ... proving that the
multiple id problem is NOT due to the repeater.

The problem arises because you have to databind, and when you do your code
behind says there are multiple instances of your dropdowns, because it is not
within the repeater and not using the repeaters inner working to do the binding.

The answer is simple really. To do it this way (i.e. using html and aspx files)
you need to use the workings of the repeater ... so you have to databind within
the repeater.


Protected Sub InsurDropDown()
'MyConnect.Open()
Dim AddChoice As ListItem
Dim MyCommand = New SqlCommand("Select * From
tCollectionAction where type  = 1 or Type = 0",
MyConnect)
oReader = MyCommand.ExecuteReader()
DD_InsurPayAction.DataSource = oReader
DD_InsurPayAction.DataBind()
MyConnect.Close()
AddChoice = New ListItem("Select Insurance
Payment Action", "0")
AddChoice.Selected = True
If Not
DD_InsurPayAction.Items.Contains(AddChoice) Then
DD_InsurPayAction.Items.Insert(0,
AddChoice)
End If
End Sub

Here .... this is OK ... but if you try to databind to oReader your dropdowns
won't see it ... you need to make a declaration of oReader at the top (page
scope).

now ... the VITAL bit .... && easy really

<asp:dropdownlist ID="DD_InsurPayAction" Font-Size="8"
DataValueField="BillingActionID" DataTextField="BillingActionDescription"
runat="server" />

change to
<asp:dropdownlist ID="DD_InsurPayAction" Font-Size="8" datasource='<%# oReader
%> runat="server" />

or you could use a helper function that returns the datasource
<asp:dropdownlist ID="DD_InsurPayAction" Font-Size="8" datasource='<%#
GetSource("DD_InsurPayAction") %> runat="server" />

public DataSet GetSource(string s){
if (s == "DD_InsurPayAction") {
..... do all your biz
and return a dataset or dataview or whatever oReader returns or whatever
BillingActionID is ??
}
}

You'll have to sort out what does get returned yourself .... but the following
works.

--------------------------------------------------------------------------------\
---------------------------------

<asp:Repeater id="Repeater1" runat="server">
<HeaderTemplate>
helo
</HeaderTemplate>
<ItemTemplate>
Order Date: <%# DataBinder.Eval(Container.DataItem, "DateTimeValue",
"{0:d}") %>
Quantity: <%# DataBinder.Eval(Container.DataItem, "IntegerValue", "{0:N2}") %>
Item: <%# DataBinder.Eval(Container.DataItem, "StringValue") %>
Order Date: <asp:CheckBox id=chk1 Checked='<%#
(bool)DataBinder.Eval(Container.DataItem, "BoolValue") %>' runat=server/>
<asp:DropDownList id="Dropdownlist1" datasource='<%# getSrc() %>'
runat="server"></asp:DropDownList>
<br><br>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
--------------------------------------------------------------------------------\
---------------------------------
protected System.Web.UI.WebControls.Repeater Repeater1;
protected System.Web.UI.WebControls.DropDownList DropDown1;
private ArrayList values;

void Page_Load(object Sender, System.EventArgs e)
{

if (!Page.IsPostBack)
{
values = new ArrayList();

values.Add ("IN");
values.Add ("KS");
values.Add ("MD");
values.Add ("MI");
values.Add ("OR");
values.Add ("TN");


DataTable dt = new DataTable();
DataRow dr;

dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
dt.Columns.Add(new DataColumn("DateTimeValue", typeof(DateTime)));
dt.Columns.Add(new DataColumn("BoolValue", typeof(bool)));

for (int i = 0; i < 9; i++)
{

dr = dt.NewRow();

dr[0] = i;
dr[1] = "Item " + i.ToString();
dr[2] = DateTime.Now;
dr[3] = (i % 2 != 0) ? true : false;

dt.Rows.Add(dr);
}

Repeater1.DataSource = new DataView(dt);
Repeater1.DataBind();
}
}

public ArrayList getSrc()
{
return values;
}



Note there are better ways to do this using ITemplate and CustomControls.

Share: 

 

This Question has 9 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Dropdown nested in repeater Or get search suggestion and latest updates.


Tagged: