Reputation: 619
I have a dropdownlist , i can see list item from 1 to 10, however when I select any one of the value it doesnt display in dropdownlist,
<asp:DropDownList ID="ddlqty" runat="server" CssClass="myselect">
<asp:ListItem Value="1" Selected="True">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
<asp:ListItem Value="5">5</asp:ListItem>
<asp:ListItem Value="6">6</asp:ListItem>
<asp:ListItem Value="7">7</asp:ListItem>
<asp:ListItem Value="8">8</asp:ListItem>
<asp:ListItem Value="9">9</asp:ListItem>
<asp:ListItem Value="10">10</asp:ListItem>
</asp:DropDownList>
what am i missing here. Also when in code behind intellisense not getting ddlqty
which is the ID of the dropdownlist.
This is the HTML I am getting from the browser:
<select name="Repeater1$ctl01$ddlqty" id="Repeater1_ddlqty_0" class="myselect">
<option selected="selected" value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
My Repeater
class
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<tr class="cart_item">
<td class="product-thumbnail">
<a href="#">
<img src=" images/ecommerce/products/hoodie_1_front-150x150.jpg" alt=""></a>
</td>
<td class="product-name"><a href="#"><%#Eval("ItemName") %></a>
<dl class="variation">
<dt class="variation-Color">Category:</dt>
<dd class="variation-Color">
<p><%#Eval("Category") %></p>
</dd>
</dl>
</td>
<td class="product-price"><span class="amount"><%#Eval("Rate") %></span></td>
<td class="product-quantity">
<%-- <a href="#" class="minus disabled">-</a>
<input type="text" value="1"> <a href="#" class="plus">+</a></td>--%>
<%--<input type="text" value="1" readonly="true">--%>
<asp:DropDownList ID="ddlqty" runat="server" CssClass="myselect">
<asp:ListItem Value="1" Selected="True">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
<asp:ListItem Value="5">5</asp:ListItem>
<asp:ListItem Value="6">6</asp:ListItem>
<asp:ListItem Value="7">7</asp:ListItem>
<asp:ListItem Value="8">8</asp:ListItem>
<asp:ListItem Value="9">9</asp:ListItem>
<asp:ListItem Value="10">10</asp:ListItem>
</asp:DropDownList>
<%--<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false" CommandName="Edit" Text="Change" Font-Size="Small"></asp:LinkButton>--%>
<td class="product-subtotal"><span class="amount">$ <%#Eval("Rate") %></span></td>
<td class="product-remove"><a href="#" class="remove"><i class="fa fa-times"></i></a></td>
</tr>
</ItemTemplate>
<FooterTemplate></FooterTemplate>
</asp:Repeater>
Please help
Upvotes: 0
Views: 2475
Reputation: 619
I am able to solve my issue, now i can see selected value in the text of dropdownlist. However i have to remove Repeater
. And used gridview instead. I am having 1 issue that when I click on Save
using <asp:LinkButton ID="lnkUpdate" runat="server" CommandName="Update" Text="Save" ></asp:LinkButton>
it refreshes the page and value again goes to 1 which is by default value.
Heres my code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="slno" ForeColor="#333333" GridLines="None" HorizontalAlign="Center" Width="100%" OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdating="GridView1_RowUpdating">
<RowStyle HorizontalAlign="Center" VerticalAlign="Middle" BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="slno" HeaderText="Sl No" ReadOnly="true">
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:BoundField>
<asp:BoundField DataField="itemname" HeaderText="Item Name" ReadOnly="true">
<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" />
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" />
</asp:BoundField>
<asp:BoundField DataField="Rate" HeaderText="Price" ReadOnly="true">
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:BoundField>
<asp:TemplateField HeaderStyle-CssClass="headerStyle" HeaderText="Quantity">
<EditItemTemplate>
<asp:DropDownList ID="ddlqty" runat="server" DataTextField='<%# Bind("qty") %>' AutoPostBack="false" EnableViewState="true">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblqty" runat="server" Text='<%# Bind("qty") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle CssClass="headerStyle" HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-CssClass="headerStyle">
<EditItemTemplate>
<asp:LinkButton ID="lnkUpdate" runat="server" CommandName="Update" Text="Save" ></asp:LinkButton>
<%--<asp:LinkButton ID="lnkCancel" runat="server" CommandName="Cancel" Text="Cancel"></asp:LinkButton>--%>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false" CommandName="Edit" Text="Change"></asp:LinkButton>
</ItemTemplate>
<HeaderStyle CssClass="headerStyle" />
<ItemStyle HorizontalAlign="Center" Width="30px" Font-Size="Small" />
</asp:TemplateField>
<asp:BoundField DataField="total" HeaderText="Total" ReadOnly="true">
<HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" />
<ItemStyle HorizontalAlign="Right" VerticalAlign="Middle" />
</asp:BoundField>
</Columns>
<EditRowStyle HorizontalAlign="Center" VerticalAlign="Middle" BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" ForeColor="#333333" Font-Bold="True" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" HorizontalAlign="Center" VerticalAlign="Middle" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
I had a previous project with me doen by a friend so used his codes.
Upvotes: 0
Reputation: 35514
When a Control is inside a Repeater, you cannot access it directly. You will have to use FindControl
with an Index number of the Item that contains the Control inside the Repeater.
DropDownList drp = Repeater1.Items[index].FindControl("ddlqty") as DropDownList;
drp.SelectedValue = "2";
UPDATE
If you want to set the DropDownList values from the cart, and update the cart when you change the items you can do this by adding OnItemDataBound
and OnSelectedIndexChanged
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<asp:DropDownList ID="ddlqty" runat="server" OnSelectedIndexChanged="ddlqty_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
</asp:DropDownList>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("tocht_id") %>' Visible="false"></asp:Label>
</ItemTemplate>
</asp:Repeater>
Code behind
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
int itemsInCart = 3;
//find the dropdownlist using findcontrol
DropDownList drp = e.Item.FindControl("ddlqty") as DropDownList;
//set the correct cart value
drp.SelectedValue = itemsInCart.ToString();
}
protected void ddlqty_SelectedIndexChanged(object sender, EventArgs e)
{
//cast the sender back to a dropdownlist
DropDownList drp = sender as DropDownList;
//get the item as a repeater item
RepeaterItem item = drp.NamingContainer as RepeaterItem;
//find the label with the product ID in the Repeater
Label lbl = Repeater1.Items[item.ItemIndex].FindControl("Label1") as Label;
//get the product id from the attri
int productID = Convert.ToInt32(lbl.Text);
//convert the selectedvalue back to an int
int itemsInCart = Convert.ToInt32(drp.SelectedValue);
//you now have the product id and the new cart amount
}
Upvotes: 1
Reputation: 420
Try to get the DropDownList
value using foreach
loop
foreach(RepeaterItem item in rpt1.Items)
{
DropDownList ddl = (DropDownList)item.FindControl("ddl");
string ddl_value = ddl.SelectedValue;
}
Note: rpt1 is Repeater's Id
also note that by using
foreach loop
you'll get alldropdownlist's
value, so you have to use it according your scenario.
Upvotes: 1
Reputation: 1617
Below is the code that will use to Bind Repeater at page load under IsPostback condition on page load event
private void BindData()
{
DataTable dt = new DataTable();
dt.Columns.Add("ID", Type.GetType("System.String"));
dt.Columns.Add("ItemName", Type.GetType("System.String"));
dt.Columns.Add("Category", Type.GetType("System.String"));
dt.Columns.Add("Rate", Type.GetType("System.String"));
dt.Columns.Add("Qty", Type.GetType("System.String"));
dt.Columns.Add("Total", Type.GetType("System.String"));
for (int i = 1; i <= 10; i++)
{
DataRow dr = dt.NewRow();
dr["ID"] = 1;
dr["ItemName"] = "hoodie";
dr["Category"] = "Hoodie";
dr["Rate"] = 25;
dr["Qty"] = 4;
dr["Total"] = (4 * 25);
dt.Rows.Add(dr);
}
Repeater1.DataSource = dt;
Repeater1.DataBind();
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DropDownList ddlQty = new DropDownList();
ddlQty = (DropDownList)e.Item.FindControl("ddlqty");
HiddenField hdn = new HiddenField();
hdn = (HiddenField)e.Item.FindControl("hdnQty");
if (hdn.Value != "")
{
ddlQty.SelectedItem.Selected = false;
ddlQty.Items.FindByValue(hdn.Value).Selected = true;
}
}
}
You need to use ItemDataBound event of Repeater that will give you control at the time of binding data to repeater and you can change value of that control while binding records.
Another thing that you have to do is to handle dropdown selected index event that will give you value of qty and you can use it to rebind repeater.
Upvotes: 0