[英]Not able to display selected value in drop down list in asp.net
我有一個下拉列表,我可以看到列表項從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>
請幫忙
當控件位於中繼器中時,您不能直接訪問它。 您將必須使用FindControl
,並將其包含在Repeater中的包含控件的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
}
嘗試使用foreach
循環獲取DropDownList
值
foreach(RepeaterItem item in rpt1.Items)
{
DropDownList ddl = (DropDownList)item.FindControl("ddl");
string ddl_value = ddl.SelectedValue;
}
注意:rpt1是中繼器的ID
還請注意,通過使用
foreach loop
您將獲得所有dropdownlist's
值,因此您必須根據自己的情況使用它。
以下是在頁面加載事件下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事件,該事件將在將數據綁定到Repeater時為您提供控件,並且可以在綁定記錄時更改該控件的值。
您要做的另一件事是處理下拉索引事件,該事件將為您提供qty的值,您可以使用它重新綁定轉發器。
我能夠解決我的問題,現在我可以在下拉列表的文本中看到選定的值。 但是,我必須刪除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>
我以前有一個朋友和我一起做過一個項目,所以用了他的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.