我有一个下拉列表,我可以看到1到10的列表项,但是当我选择其中任何一个值时,它不会显示在下拉列表中,
<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>
我在这里失踪了什么。此外,在intellisense后面的代码中,不会得到ddlqty
,这是下拉列表的ID。
这是我从浏览器中获取的HTML:
<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>
我的Repeater
班级
<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>
请帮忙
答案 0 :(得分:1)
当控件位于中继器内时,您无法直接访问它。您必须使用FindControl
,其中包含在Repeater中包含Control的Item的索引号。
DropDownList drp = Repeater1.Items[index].FindControl("ddlqty") as DropDownList;
drp.SelectedValue = "2";
更新
如果您想从购物车设置DropDownList值,并在更改商品时更新购物车,可以通过添加OnItemDataBound
和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>
背后的代码
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
}
答案 1 :(得分:1)
尝试使用DropDownList
循环
foreach
值
foreach(RepeaterItem item in rpt1.Items)
{
DropDownList ddl = (DropDownList)item.FindControl("ddl");
string ddl_value = ddl.SelectedValue;
}
注意:rpt1是Repeater的Id
另请注意,使用
foreach loop
您将获得所有dropdownlist's
价值,所以你必须根据你的情况使用它。
答案 2 :(得分:0)
以下是在页面加载事件下IsPostback条件下页面加载时用于绑定中继器的代码
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;
}
}
}
您需要使用Repeater的ItemDataBound事件,它可以在将数据绑定到转发器时为您提供控制,并且您可以在绑定记录时更改该控件的值。
你需要做的另一件事是处理下拉选择的索引事件,它会给你qty的值,你可以用它来重新绑定转发器。
答案 3 :(得分:0)
我能够解决我的问题,现在我可以在下拉列表的文本中看到选定的值。但是我必须删除Repeater
。并使用gridview代替。我遇到1个问题,当我使用Save
点击<asp:LinkButton ID="lnkUpdate" runat="server" CommandName="Update" Text="Save" ></asp:LinkButton>
时,它会刷新页面,并且值再次变为1,这是默认值。
继承我的代码:
<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>
我和朋友做了一个以前的项目,所以使用了他的代码。