473,837 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DropdownList in Gridview won't select the correct value

1 New Member
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"
AutoGenerateCol umns="False"
CssClass="Grid"
OnRowDataBound= "Grid_RowDataBo und"
DataKeyNames="S ubProdID"
OnSelectedIndex Changed="Grid_S electedIndexCha nged">

<Columns>

<asp:TemplateFi eld HeaderText="ID" >

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



<asp:TemplateFi eld HeaderText="Nam e" >

<ItemTemplate >
<asp:Label ID="Label2" runat="server" Text='<%# Bind("SubProdNa me") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateFie ld>


<asp:TemplateFi eld HeaderText="Des c">
<ItemTemplate >
<asp:Label ID="Label3" runat="server" Text='<%# Bind("SubProdDe sc") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateFie ld>

<asp:TemplateFi eld HeaderText="Opt ions" >
<ItemTemplate >
<asp:DropDownLi st ID="DDL" runat="server" Width="60">
</asp:DropDownLis t>
</ItemTemplate>
</asp:TemplateFie ld>

<asp:BoundFie ld
HeaderText="Pri ce"
DataField="SubP rodPrice"
DataFormatStrin g="{0:C}"
ReadOnly="True"
HtmlEncode="Fal se" />

<asp:ButtonFiel d
ButtonType="But ton"
CommandName="Se lect"
Text="Add to cart">
<ControlStyle CssClass="Small ButtonText" />
</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 GetSubProductTe st2 : System.Web.UI.P age
{

string productId = "35";

List<SubProduct Wrapper> SPWlist = new List<SubProduct Wrapper>();



protected void Page_Load(objec t sender, EventArgs e)
{

List<SubProduct Wrapper> SubProduct = SubProductAcces s.GetSubProduct s(productId);

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

}



protected void Grid_RowDataBou nd(object sender, GridViewRowEven tArgs e)
{

if (e.Row.RowType == DataControlRowT ype.DataRow)
{


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

int SubProdID2 = Int32.Parse(Sub ProdID1);

foreach (SubProductWrap per SubProd in SPWlist)
{

List<SubProduct OptionsWrapper> SubProdOptions = (SubProd.SubPro dOptions);

foreach (SubProductOpti onsWrapper options in SubProdOptions)
{

int SubProdID = options.SubProd ID;
int OptionID = options.OptionI D;
string OptionName = options.OptionN ame;

DropDownList ddl = (DropDownList)e .Row.FindContro l("DDL");

ddl.DataSource = SubProdOptions;
ddl.DataTextFie ld = "OptionName ";
ddl.DataValueFi eld = "OptionID";
ddl.SelectedVal ue = OptionName;



if (SubProdID2 == SubProdID)
{
ddl.Items.Add(n ew ListItem(Option Name));
}

}
}
}

}



protected void Grid_SelectedIn dexChanged(obje ct sender, EventArgs e)
{
GridViewRow row = Grid.SelectedRo w;

int id = (int)Grid.Selec tedDataKey.Valu es["SubProdID"];
string name = Grid.SelectedRo w.Cells[1].Text;
string desc = Grid.SelectedRo w.Cells[2].Text;


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

string price = Grid.SelectedRo w.Cells[4].Text;

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

}
}
Dec 30 '07 #1
0 1813

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

Similar topics

2
2155
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 instance, if I'm working with a table of hotels, I might want a 'select' control as follows <select id="mylistbox" runat="server"> <option value="*">Add New Hotel</option> <option value="1">The Hilton</option> <option value="2">The...
4
4497
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 tables, a table of cars (pretty basic, an ID, a description, and a Color ID) and a table of colors (Color ID, and a color description). I've added a gridview and a detailsview as I'm playing with both and how to get editing features to work the way I...
6
8282
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 trying to load the ddl with a list of dates from another table keyed on GridView row field that only apply to this row. Any suggestions would be appreciated.
0
1624
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 field 'color', the selectedvalue of a dropdownlist must be used, containing all possible colors (red, blue ...), so the field 'color' is ReadOnly in the gridview. Everything works, but i still have 2 problems: 1)the new color in the field is...
6
1483
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 can add a "Select Item," but is that the correct way to do it? I don't want it to hit the database at all the first time the page loads. I have done everything through visual studio and was trying to see if I could do this with no code behind. ...
1
7768
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 label and hours, along with an edit linkbutton. When they click on edit, the edit button turns to Update Cancel TimeID stays read Only
3
1653
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
2712
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, which I had trouble with in the past (i.e. same problem), is now partly broke. The paging no longer fully works in the gridview. The gvAwarded_RowDataBound code works to load the ddlPager with the correct number of pages.
0
2228
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 ParkingBay CommandField Me Y dropdownlistvalue CommandField You m dropdownlistvalue i have 2 sqldatasource. the other one is for gridview and...
0
9682
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10881
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10275
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9406
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7807
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7004
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5670
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5850
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3126
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.