473,399 Members | 3,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

DropdownList in Gridview won't select the correct value

Hi there,
I have a Gridview control which has a dropdownlist in every row. The dropdowns are populated dynamically from a database (so the content is different in each row. There is also a select button for each row so that the the user can choose a row and the values will be added to a shopping cart.

My problem is that the page loads fine and everything is showing as it should be. However, when I select a row the dropdownlist for that row always selects the first item in the list (regardless of the option I selected). This is really frustrating because it took so long to get the page to bind, I really thought this would be the easy part. Here's the code so you can see where I'm at so far:

<asp:GridView ID="Grid" runat="server"
AutoGenerateColumns="False"
CssClass="Grid"
OnRowDataBound="Grid_RowDataBound"
DataKeyNames="SubProdID"
OnSelectedIndexChanged="Grid_SelectedIndexChanged" >

<Columns>

<asp:TemplateField HeaderText="ID">

<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("SubProdID") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>



<asp:TemplateField HeaderText="Name" >

<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("SubProdName") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>


<asp:TemplateField HeaderText="Desc">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("SubProdDesc") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Options" >
<ItemTemplate>
<asp:DropDownList ID="DDL" runat="server" Width="60">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>

<asp:BoundField
HeaderText="Price"
DataField="SubProdPrice"
DataFormatString="{0:C}"
ReadOnly="True"
HtmlEncode="False" />

<asp:ButtonField
ButtonType="Button"
CommandName="Select"
Text="Add to cart">
<ControlStyle CssClass="SmallButtonText" />
</asp:ButtonField>

</Columns>
</asp:GridView>


<asp:Label ID="LblId" runat="server"></asp:Label>
<br />
<asp:Label ID="LblName" runat="server"></asp:Label>
<br />
<asp:Label ID="LblDesc" runat="server"></asp:Label>
<br />
<asp:Label ID="LblOption" runat="server"></asp:Label>
<br />
<asp:Label ID="LblPrice" runat="server"></asp:Label>


*****code behind*******

public partial class GetSubProductTest2 : System.Web.UI.Page
{

string productId = "35";

List<SubProductWrapper> SPWlist = new List<SubProductWrapper>();



protected void Page_Load(object sender, EventArgs e)
{

List<SubProductWrapper> SubProduct = SubProductAccess.GetSubProducts(productId);

SPWlist = SubProduct;
Grid.DataSource = SPWlist;
Grid.DataBind();

}



protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{


string SubProdID1 = Grid.DataKeys[e.Row.DataItemIndex].Value.ToString();

int SubProdID2 = Int32.Parse(SubProdID1);

foreach (SubProductWrapper SubProd in SPWlist)
{

List<SubProductOptionsWrapper> SubProdOptions = (SubProd.SubProdOptions);

foreach (SubProductOptionsWrapper options in SubProdOptions)
{

int SubProdID = options.SubProdID;
int OptionID = options.OptionID;
string OptionName = options.OptionName;

DropDownList ddl = (DropDownList)e.Row.FindControl("DDL");

ddl.DataSource = SubProdOptions;
ddl.DataTextField = "OptionName";
ddl.DataValueField = "OptionID";
ddl.SelectedValue = OptionName;



if (SubProdID2 == SubProdID)
{
ddl.Items.Add(new ListItem(OptionName));
}

}
}
}

}



protected void Grid_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = Grid.SelectedRow;

int id = (int)Grid.SelectedDataKey.Values["SubProdID"];
string name = Grid.SelectedRow.Cells[1].Text;
string desc = Grid.SelectedRow.Cells[2].Text;


DropDownList dr = ((DropDownList)row.FindControl("DDL"));
int index = dr.SelectedIndex;
string option = dr.Items[index].Value;

string price = Grid.SelectedRow.Cells[4].Text;

LblId.Text = id.ToString();
LblName.Text = name;
LblDesc.Text = desc;
LblOption.Text = option;
LblPrice.Text = price;

}
}
Dec 30 '07 #1
0 1764

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: COHENMARVIN | last post by:
I'm leafing through a big book on asp.net, and I don't see any way to the following: 1. bind values and text to a dropdown 2. Also make the first line of the dropdown say something different. For...
4
by: P. Yanzick | last post by:
Hello, I've been playing with master/detail views as well as editing in the gridview, and I ran across a strange problem that I am not exactly sure where to go to try to solve. I have 2...
6
by: Dabbler | last post by:
I have a dropdownlist in a GridView ItemTemplate. I need to bind the ddl to an SqlDataSource, then have a value from a boundfield in the row be passed as the keyfield for select where clause. Im...
0
by: Ben | last post by:
Hi, It's about a database containing (for simplifying) three fields: 'placenr' (primary key), 'place' and 'color'. The records are updatable (Edit/Update button) in a gridview. For updating the...
6
by: needin4mation | last post by:
I have a gridview and a dropdownlist. If I change the ddl, the gridview populates. That is what I want. But what I don't want is for it to happen the first time they get to the page. I know I...
1
by: mitchman10 | last post by:
My Time table has TimeID,Employee,PayPeriod,ChargeCodeID,Hours My Chargecode table has ChargecodeID,c_Text I need an Editable datagrid that will show the TimeID,Employee,PayPeriod,C_Text in a...
3
by: Jon Paal | last post by:
this text keeps showing up in my gridview dropdownlist "System.Data.DataRowView" How do I prevent this problem ?
1
by: mark4asp | last post by:
I moved a page to another web-site and now it's broke! I had 5 pages in their own web-site. These pages have now been moved to another web-site. Everything is fine except that one of the pages,...
0
by: lamolap | last post by:
i have 1 gridview , a dropdownlist inside a gridview and a commandfield of (edit, update and cancel) my gidview looks like this Edit Surname Initials ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.