473,325 Members | 2,870 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,325 software developers and data experts.

Having issues with DropDownList & code behind - ASP.NET

2
This is my first post, so excuse me if I don't get this right the first time. I have an issue where when I use DropDownList I only get the first, or "SelectedValue" back when I update a GridView Edit. I have been trying to hunt down a viable soltion to this, seems that many people out there have similar issues and the ...SelectedItem.Value or SelectedValue is not returning what is actually being selected by the end user. So the simple question is, what am I doing wrong?? This is only part of the code, I tried to simply this so it doesn't look like a ton of stuff here.

Thanks for any advice...

Aspx info first....
<asp:GridView ID="GridView1"
runat="server"
CellPadding="3"
HorizontalAlign="Left"
AllowPaging="True"
AllowSorting="True"
PageSize="30"
OnRowCreated="OnRowCreated"
OnPageIndexChanging="GridView1_PageIndexChanging"
OnSorting="GridView1_Sorting"
OnRowEditing="GridView1_RowEditing"
OnRowCancelingEdit="GridView1_CancelEditing"
OnRowUpdating="GridView1_Updating"
AutoGenerateColumns="false" >
<Columns>
... More Columns ...
<asp:TemplateField
HeaderText="Proj. Coord."
SortExpression="proj_c"
ItemStyle-VerticalAlign="Top"
ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label
ID="proj_c"
Text='<%# Eval("proj_c") %>'
Runat="Server"/>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList
ID="list1"
DataSource='<%# FillList1() %>'
DataTextField="em_name"
DataValueField="em_num"
AppendDataBoundItems="true"
SelectedValue='<%# Bind("proj_c_num") %>'
width="110px"
height="35px"
runat="server" >
<asp:ListItem Value="">(None)</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
... More Columns ...
<asp:CommandField
EditText="Edit"
CancelText="Can"
UpdateText="Upd"
ButtonType="Link"
ShowEditButton="True" />


Code Behind.
protected DataSet FillList1()
{
string pc = Request.Form["proj_c"];
SqlConnection ConnDB = GetConnection();
try
{
SqlCommand sql5;
ConnDB.Open();
// Run stored procedure.
sql5 = new SqlCommand("qsoAllProjCoords00", ConnDB);
sql5.CommandType = CommandType.StoredProcedure;
DataSet ds1 = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(sql5);
sda.Fill(ds1);
return ds1;
ConnDB.Close();
}
finally
{
ConnDB.Close();
}
return null;
}

protected void GridView1_Updating(Object sender, GridViewUpdateEventArgs e)
{
string proj_c = String.Empty;
if (Page.IsPostBack)
{
proj_c = ((DropDownList)((GridView)sender).Rows[e.RowIndex].FindControl("list1")).SelectedItem.Value;
}
else
{ return; }
SqlConnection ConnDB = GetConnection();
try
{
... Sql Stored Procedure here...
Loads the proj_c and many other things which work fine, except for the proj_c is
NOT the right selection....
}
finally
{
ConnDB.Close();
}
populateGridView(); // Display new updated table.
GridView1.EditIndex = -1; // Turn off edit mode.
GridView1_ReBind(GridView1.EditIndex);
}

Is it possible that I need something to do with the SelectedIndex to go along with the SelectedValue?? I was also thinking that there maybe an issue with ((DropDownList).... part, it appears to me that it looks like a new DropDownList is being created, thus wipping out the info from the list coming from the aspx file, is that just how it appears to me?? I haven't been successful with DropDownList yet, in an update.

Thanks.
Jan 31 '08 #1
3 2345
myth0s
32
This looks like a "IsPostback" issue.

Depending on how your code behing file is done, you may be "overwritting" changes the user did everytime you send the file back to the server.

When the user load the page the first time, your code fills the list. The user can then choose an item and click "send" or whatever. When the code behind file is executed, it re-fills the list (and by doing so, the user selection is lost).

In pseudo-code, your code-behind should look like this :

Expand|Select|Wrap|Line Numbers
  1. Sub PageLoad()
  2.    If not Page.IsPostback then
  3.       FillList()
  4.    Else
  5.       GetSelection()
  6.    End If
  7. End sub
I understand you may not be using a "Page load" event, but testing for Page.IsPostBack before calling FillList1 may help.

For more information on post back, please read : http://aspnet101.com/aspnet101/tutorials.aspx?id=3

I hope this helps
Feb 1 '08 #2
RPhlb
2
No it does not, if I code a
if (!IsPostBack) { return;}
into the FillList1() it has no idea what the Postback is, I get an error, so it is not being posted back. The FillList is only being done by the DataSource from the DropDownList. I had thought about this before also, I actually tried this in the GridView1_Updating part, but it also had no positive effect.

This should not be this difficult, seems that everything I attempt to do returns the exact same result, that is why I was thinking my issue is with the aspx part and not the code behind. But I have been wrong about this the whole time so far, but I see this exact same problem all over the net and every solution I've tried including ones from this site don't work. So I am loosing hope of using the DropDownList or a ListBox and I am afraid I will have to come up with some other solution, it shouldn't take 5-6 days of searching to find a resolution to this problem.

So I continue to search for solutions for this one. I don't understand how the simple SelectedValue or SelectedItem.Value don't return what is being selected at all, isn't it supposed to do that??

Thank you for your help.
Feb 1 '08 #3
myth0s
32
I have a feeling (I may be very wrong) that the following bit of code is causing you a lot of trouble :
Expand|Select|Wrap|Line Numbers
  1. <asp:DropDownList
  2. ID="list1"
  3. DataSource='<%# FillList1() %>
When I think about it, everytime your page is loaded / refreshed / etc., the FillList1() method will be executed and will wipe user's selection.

Perhaps you could try to call FillList1() from a page load event using Page.IsPostBack. Else, you probably need to find a way to prevent the list from re-querying it's DataSource everytime...

That's my feeling. Good luck.
Feb 1 '08 #4

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

Similar topics

5
by: Bob N | last post by:
I have an ASP.NET web page -- several auto-post back DropDownLists that perform a query against a relatively large database (3 to 4 seconds delay) that repopulate a couple of datagrids, comboboxes...
1
by: DC Gringo | last post by:
Using code-behind, I have a page "index.aspx" that has a user control mainHeader.ascx (with a language selection dropdownlist). Upon selecting a language from the dropdownlist, it should post...
7
by: David Laub | last post by:
I have stumbled across various Netscape issues, none of which appear to be solvable by tweaking the clientTarget or targetSchema properties. At this point, I'm not even interested in "solving"...
4
by: Jeff | last post by:
Help.... I have formatted a lsit of strings to add to a dropdownlist to shows three colums: id, description and price. i.e. 123 description here $12.95...
15
by: Swetha | last post by:
Hello I have a DropDownList that I am populating using the following SqlDataSource: <asp:DropDownList ID="parentIDDropDownList" runat="server" DataSourceID="SqlDataSource3"...
5
by: Mike | last post by:
In ASP.NET 2.0 I have a dropdownlist populated from a sqldatasource. The dropdownlist values are successfully populated from a database table. I would like to add the value "Select Company" which...
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...
2
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, i'm in vs2005 designer view editing a formview's edit template. i have a dropdownlist and i'm trying to bind the SelectedValue to a custom expression. i was wondering if it is possible to...
5
by: nzkks | last post by:
Hi I am using these: ASP.Net 2.0, VB.Net, Visual Studio 2005, SQL Server 2005, Formview controls In a ASP.Net form I have 20 textboxes and 20 dropdownlists(ddl). All ddl(s) are databound and get...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.