468,278 Members | 1,571 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,278 developers. It's quick & easy.

Assign SelectedValue to DropDownList from code-behind file using SqlDataSource

Hello

I have a DropDownList that I am populating using the following
SqlDataSource:

<asp:DropDownList ID="parentIDDropDownList" runat="server"
DataSourceID="SqlDataSource3" DataTextField="name"
DataValueField="ID"></asp:DropDownList><asp:SqlDataSource
ID="SqlDataSource3" runat="server" ConnectionString="<%$
ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ID], [name] FROM [client]
ORDER BY [name]"></asp:SqlDataSource>

Before I assign SelectedValue for this DropDownList, I need to add
another Item to it. This I do in the FormView's DataBound EventHandler.

protected void FormView1_DataBound(object sender, EventArgs e)
{
if(FormView1.CurrentMode == FormViewMode.Edit)
{
DropDownList myDdl =
(DropDownList)(FormView1.FindControl("parentIDDrop DownList"));

if (myDdl != null)
{
myDdl.Items.Insert((myDdl.Items.Count), new
ListItem("Select a parent", "0"));
// Wish to assign myDdl.SelectedValue here
// myDdl.SelectedValue=SqlDataSource3.????
}
}
}

After inserting the "Select a Parent" Item, I wish to assign the
SelectedValue of the DropDownList to the corresponding value returned
by SqlDataSource3. How can I do this?

Thanks!

Nov 19 '05 #1
15 26539
Swetha,

Just convert the value returned by the datasource to a string and assign it.
SelectedValue gets or sets the selected value as a string.

myDdl.SelectedValue=dataSourceValue;

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Swetha" <sw***********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hello

I have a DropDownList that I am populating using the following
SqlDataSource:

<asp:DropDownList ID="parentIDDropDownList" runat="server"
DataSourceID="SqlDataSource3" DataTextField="name"
DataValueField="ID"></asp:DropDownList><asp:SqlDataSource
ID="SqlDataSource3" runat="server" ConnectionString="<%$
ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ID], [name] FROM [client]
ORDER BY [name]"></asp:SqlDataSource>

Before I assign SelectedValue for this DropDownList, I need to add
another Item to it. This I do in the FormView's DataBound EventHandler.

protected void FormView1_DataBound(object sender, EventArgs e)
{
if(FormView1.CurrentMode == FormViewMode.Edit)
{
DropDownList myDdl =
(DropDownList)(FormView1.FindControl("parentIDDrop DownList"));

if (myDdl != null)
{
myDdl.Items.Insert((myDdl.Items.Count), new
ListItem("Select a parent", "0"));
// Wish to assign myDdl.SelectedValue here
// myDdl.SelectedValue=SqlDataSource3.????
}
}
}

After inserting the "Select a Parent" Item, I wish to assign the
SelectedValue of the DropDownList to the corresponding value returned
by SqlDataSource3. How can I do this?

Thanks!

Nov 19 '05 #2
Hi Justin,

I think I wasnt clear enough in my previous mail:

The DropDownList gets populated with SqlDataSource3. But the
SelectedValue comes from another SqlDataSource - SqlDataSource1.

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:Plus32ConnectionString %>"
SelectCommand="Client_getDetails"
SelectCommandType="StoredProcedure" >
<SelectParameters>
<asp:QueryStringParameter Name="ID"
QueryStringField="clientID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>

This stored procedure returns a host of values of which I want one
particular int field "parentID"

Hence in my code behind file, how do I access the specific parameter
value for "parentID"?

myDdl.SelectedValue=SqlDataSource1.??

Thanks
Swetha

Nov 19 '05 #3
Swetha,

Ok, I understand now.

If there is only one row in the datasource (I'm assuming the datasource is a
dataset here.) then you'll need to specify which item in the populated
datasource to get: If you are retrieving the data from the datasource and it
only has one table and one row it would be retrieved like this:

SqlDatasource1.Tables(0).Rows(0).Item("parentId"). ToString

Tables, rows, and columns may be accessed by their zero based index or by a
string name. In the case above I'm accessing the first table - first row -
and parentId column by name.

Does your datasource have more than one table and/or more than one row? If
that's the case let me know a bit more about it's structure and I can give
you more detail on accessing the data you need if this wasn't enough
information.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Swetha" <sw***********@gmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
Hi Justin,

I think I wasnt clear enough in my previous mail:

The DropDownList gets populated with SqlDataSource3. But the
SelectedValue comes from another SqlDataSource - SqlDataSource1.

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:Plus32ConnectionString %>"
SelectCommand="Client_getDetails"
SelectCommandType="StoredProcedure" >
<SelectParameters>
<asp:QueryStringParameter Name="ID"
QueryStringField="clientID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>

This stored procedure returns a host of values of which I want one
particular int field "parentID"

Hence in my code behind file, how do I access the specific parameter
value for "parentID"?

myDdl.SelectedValue=SqlDataSource1.??

Thanks
Swetha

Nov 19 '05 #4
The Stored Procedure is something like:

SELECT name contact, parentID FROM sometable WHERE value = @ID

and the datasource that uses this is:

<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:Plus32ConnectionString %>"
SelectCommand="Client_getDetails"
SelectCommandType="StoredProcedure" >
<SelectParameters>
<asp:QueryStringParameter Name="ID"
QueryStringField="clientID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>

This is the DataSource that I am using to bind the SelectedValue of the
DropDownList mentioned earlier.

When I'm populating my FormView:

<asp:FormView ID="FormView1" runat="server"
DataSourceID="SqlDataSource1"
OnModeChanged="FormView1_ModeChanged" >
<EditItemTemplate>
Name:
<asp:TextBox ID="nameTextBox" runat="server" Text='<%#
Bind("name") %>'></asp:TextBox><br />
Contact Person:
<asp:TextBox ID="contactpersonTextBox" runat="server"
Text='<%# Bind("contactperson") %>'></asp:TextBox><br />

Parent:
<asp:DropDownList ID="parentIDDropDownList"
runat="server" DataSourceID="SqlDataSource3" DataTextField="name"
DataValueField="ID">
<asp:ListItem Text="Select a Parent" Value="0" />
</asp:DropDownList>

<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ID], [name] FROM [client]
ORDER BY [name]"></asp:SqlDataSource>
<br />
<asp:LinkButton ID="UpdateButton" runat="server"
CausesValidation="True" CommandName="Update"

Text="Update"></asp:LinkButton>&nbsp;<asp:LinkButton
ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
Name:
<asp:Label ID="nameLabel" runat="server" Text='<%#
Bind("clientname") %>'></asp:Label><br />
Contact Person:
<asp:Label ID="contactpersonLabel" runat="server"
Text='<%# Bind("contactperson") %>'></asp:Label><br />
Parent:
<asp:Label ID="parentIDLabel" runat="server"
</asp:Label><br />

<asp:LinkButton ID="EditButton" runat="server"
CausesValidation="False" CommandName="Edit" Visible="false"
Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:FormView>

As you can see in the EditItemTemplate, for the parentIDDropDownList, I
need to add a new ListItem with value "0", before binding the
SelectedValue to the value returned from SqlDataSource3. But once I add
the ListItem "Select a Parent", there is no way I can assign the
SelectedValue other than in the FormView1_ModeChanged() EventHandler in
the code-behind file.(Or is there???)

protected void FormView1_ModeChanged(object sender, EventArgs e)
{
if (FormView1.CurrentMode == FormViewMode.Edit)
{
DropDownList myDdl =
(DropDownList)(FormView1.FindControl("parentIDDrop DownList"));

if (myDdl != null)
{
myDdl.Items.Insert((myDdl.Items.Count), new
ListItem("Select a parent", "0"));
// myDdl.SelectedValue=SqlDataSource1.????
}
}
}

Nov 19 '05 #5
Swetha,

I had misunderstood a little of exactly what you were trying to do. The
easiest way I can think of to do this would be to databind a hidden input
server control to the id you need to retrieve.

Then using the page's PreRender event which will fire after databinding has
occurred you could retrieve that value from the hidden input and use it to
set the selected value of your drop down.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Swetha" <sw***********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
The Stored Procedure is something like:

SELECT name contact, parentID FROM sometable WHERE value = @ID

and the datasource that uses this is:

<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:Plus32ConnectionString %>"
SelectCommand="Client_getDetails"
SelectCommandType="StoredProcedure" >
<SelectParameters>
<asp:QueryStringParameter Name="ID"
QueryStringField="clientID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>

This is the DataSource that I am using to bind the SelectedValue of the
DropDownList mentioned earlier.

When I'm populating my FormView:

<asp:FormView ID="FormView1" runat="server"
DataSourceID="SqlDataSource1"
OnModeChanged="FormView1_ModeChanged" >
<EditItemTemplate>
Name:
<asp:TextBox ID="nameTextBox" runat="server" Text='<%#
Bind("name") %>'></asp:TextBox><br />
Contact Person:
<asp:TextBox ID="contactpersonTextBox" runat="server"
Text='<%# Bind("contactperson") %>'></asp:TextBox><br />

Parent:
<asp:DropDownList ID="parentIDDropDownList"
runat="server" DataSourceID="SqlDataSource3" DataTextField="name"
DataValueField="ID">
<asp:ListItem Text="Select a Parent" Value="0" />
</asp:DropDownList>

<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ID], [name] FROM [client]
ORDER BY [name]"></asp:SqlDataSource>
<br />
<asp:LinkButton ID="UpdateButton" runat="server"
CausesValidation="True" CommandName="Update"

Text="Update"></asp:LinkButton>&nbsp;<asp:LinkButton
ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
Name:
<asp:Label ID="nameLabel" runat="server" Text='<%#
Bind("clientname") %>'></asp:Label><br />
Contact Person:
<asp:Label ID="contactpersonLabel" runat="server"
Text='<%# Bind("contactperson") %>'></asp:Label><br />
Parent:
<asp:Label ID="parentIDLabel" runat="server"
</asp:Label><br />

<asp:LinkButton ID="EditButton" runat="server"
CausesValidation="False" CommandName="Edit" Visible="false"
Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:FormView>

As you can see in the EditItemTemplate, for the parentIDDropDownList, I
need to add a new ListItem with value "0", before binding the
SelectedValue to the value returned from SqlDataSource3. But once I add
the ListItem "Select a Parent", there is no way I can assign the
SelectedValue other than in the FormView1_ModeChanged() EventHandler in
the code-behind file.(Or is there???)

protected void FormView1_ModeChanged(object sender, EventArgs e)
{
if (FormView1.CurrentMode == FormViewMode.Edit)
{
DropDownList myDdl =
(DropDownList)(FormView1.FindControl("parentIDDrop DownList"));

if (myDdl != null)
{
myDdl.Items.Insert((myDdl.Items.Count), new
ListItem("Select a parent", "0"));
// myDdl.SelectedValue=SqlDataSource1.????
}
}
}

Nov 19 '05 #6
Justin

I'm trying to do what you suggested above, but for some reason the
Hidden input control is not being assigned the databound value.

Outside the <asp:FormView>, I've added the following:
.....</asp:FormView>
<asp:HiddenField ID="HiddenParentID" value='<%# Bind("parentID") %>'
runat="server" />
<asp:SqlDataSource ID="ds1"....

But when I print the value of this hidden input in the PreRender, all I
get is a blank value.

protected void FormView1_PreRender(object sender, EventArgs e)
{
Debug.WriteLine("Hidden input value is " +
HiddenParentID.Value);
}

However, the same parameter when databound to a control within the
FormView shows up with the appropriate value:

<asp:FormView DataSourceID="ds1">
<ItemTemplate>
<asp:Label ID="parentLabel" runat="server" Text='<%#
Bind("parentID") %>'>' ></asp:Label><br />
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="ds1" runat="server" ConnectionString="<%$
ConnectionStrings:Plus32ConnectionString %>"
SelectCommand="Plus32_client_getDetails"
SelectCommandType="StoredProcedure"
<SelectParameters>
<asp:QueryStringParameter Name="ID"
QueryStringField="clientID" Type="Int32" />
</SelectParameters>

I'm not able to figure out what I'm doing wrong here. I'm sure its
something silly. Could you help?

Swetha

Nov 19 '05 #7
Swetha,

I've only just begun exploring the depths of .NET 2.0 myself. I could
certainly be wrong about the data already being bound when the pre-render
fires...

You'll need to find an event that fires either at the time the data is being
bound or that fires after the data is bound to do this. I thought pre-render
would be it. But the architecture is the same. I'll take a look around and
see if I can find a mapping of the events in MSDN to find the correct one.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Swetha" <sw***********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Justin

I'm trying to do what you suggested above, but for some reason the
Hidden input control is not being assigned the databound value.

Outside the <asp:FormView>, I've added the following:
....</asp:FormView>
<asp:HiddenField ID="HiddenParentID" value='<%# Bind("parentID") %>'
runat="server" />
<asp:SqlDataSource ID="ds1"....

But when I print the value of this hidden input in the PreRender, all I
get is a blank value.

protected void FormView1_PreRender(object sender, EventArgs e)
{
Debug.WriteLine("Hidden input value is " +
HiddenParentID.Value);
}

However, the same parameter when databound to a control within the
FormView shows up with the appropriate value:

<asp:FormView DataSourceID="ds1">
<ItemTemplate>
<asp:Label ID="parentLabel" runat="server" Text='<%#
Bind("parentID") %>'>' ></asp:Label><br />
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="ds1" runat="server" ConnectionString="<%$
ConnectionStrings:Plus32ConnectionString %>"
SelectCommand="Plus32_client_getDetails"
SelectCommandType="StoredProcedure"
<SelectParameters>
<asp:QueryStringParameter Name="ID"
QueryStringField="clientID" Type="Int32" />
</SelectParameters>

I'm not able to figure out what I'm doing wrong here. I'm sure its
something silly. Could you help?

Swetha

Nov 19 '05 #8
The data is bound when the pre-render event is fired. I'm able to print
out the databound values of other controls in my FormView in
pre-render. But like I said for some reason, the Hidden Input Control
does not get assigned the value that it is bound to. I think there's
something wrong with the way I'm binding the value, though I dunno
what!!

Nov 19 '05 #9
Ok,

May I see how you're binding that data? Maybe I'll spot the problem...

This is getting interesting! : )

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Swetha" <sw***********@gmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
The data is bound when the pre-render event is fired. I'm able to print
out the databound values of other controls in my FormView in
pre-render. But like I said for some reason, the Hidden Input Control
does not get assigned the value that it is bound to. I think there's
something wrong with the way I'm binding the value, though I dunno
what!!

Nov 19 '05 #10
Following is my .aspx file:

<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="ClientDetails.aspx.cs" Inherits="ClientDetails" %>

<html >
<head runat="server">
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FormView ID="FormView1" runat="server"
DataSourceID="SqlDataSource1" OnPreRender="FormView1_PreRender"
OnDataBound="FormView1_DataBound" >
<ItemTemplate>
Name:
<asp:Label ID="nameLabel" runat="server" Text='<%#
Bind("clientname") %>'></asp:Label><br />
Parent:
<asp:Label ID="parentLabel" runat="server" Text='<%#
Bind("parentID") %>'>' ></asp:Label><br />
<asp:LinkButton ID="EditButton" runat="server"
CausesValidation="False" CommandName="Edit" Visible="false"
Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:FormView>
<asp:HiddenField ID="HiddenParentID" value='<%#
Bind("parentID") %>' runat="server" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:Plus32ConnectionString %>"
SelectCommand="client_getDetails"
SelectCommandType="StoredProcedure" >
<SelectParameters>
<asp:QueryStringParameter Name="ID"
QueryStringField="clientID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<br />
</form>
</body>
</html>
The stored procedure client_getDetails is as follows:

Select name, parentID from sometable where ID=@somevalue
And here's the code-behind file:

public partial class ClientDetails : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// some stuff on initialization
}
}

protected void FormView1_PreRender(object sender, EventArgs e)
{
if(FormView1.CurrentMode == FormViewMode.ReadOnly)
{
//The following line shows that the hidden field has no
value
Debug.WriteLine("Hidden field value is " +
HiddenParentID.Value);

Label myLbl =
(Label)(FormView1.FindControl("parentLabel"));

//This label however which is bound to the same parameter
contains the value
Debug.WriteLine("Label text " + myLbl.Text);
}
}

protected void FormView1_DataBound(object sender, EventArgs e)
{
// This also prints no value for HiddenParentID.Value
Debug.WriteLine("FormView1_DataBound - Hidden field is " +
HiddenParentID.Value);
}
}

The databinding not happening for the hidden field is understandable,
as it is not part of the FormView. And hence, it shows up with no value
in the FormView's DataBound and PreRender events. But how else can I
force the hidden field to databind?

Thanks
Swetha

S. Justin Gengo wrote:
Ok,

May I see how you're binding that data? Maybe I'll spot the problem...

This is getting interesting! : )

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Swetha" <sw***********@gmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
The data is bound when the pre-render event is fired. I'm able to print
out the databound values of other controls in my FormView in
pre-render. But like I said for some reason, the Hidden Input Control
does not get assigned the value that it is bound to. I think there's
something wrong with the way I'm binding the value, though I dunno
what!!


Nov 19 '05 #11
I just tried something more...

I added the following to the HiddenField:

<asp:HiddenField ID="HiddenParentID"
OnDataBinding="HiddenField_DataBinding"
OnPreRender="HiddenField_PreRender" value='<%# Bind("parentID") %>'
runat="server" />

protected void HiddenField_PreRender(object sender, EventArgs e)
{
Debug.WriteLine("My value is " + HiddenParentID.Value);
}

protected void HiddenField_DataBinding(object sender, EventArgs e)
{
Debug.WriteLine("I am databinding.....");
}

AND.... The HiddenField_DataBinding is never fired!!

Nov 19 '05 #12
Swetha,

Try putting the hidden field inside the form view.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Swetha" <sw***********@gmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
I just tried something more...

I added the following to the HiddenField:

<asp:HiddenField ID="HiddenParentID"
OnDataBinding="HiddenField_DataBinding"
OnPreRender="HiddenField_PreRender" value='<%# Bind("parentID") %>'
runat="server" />

protected void HiddenField_PreRender(object sender, EventArgs e)
{
Debug.WriteLine("My value is " + HiddenParentID.Value);
}

protected void HiddenField_DataBinding(object sender, EventArgs e)
{
Debug.WriteLine("I am databinding.....");
}

AND.... The HiddenField_DataBinding is never fired!!

Nov 19 '05 #13
Putting it inside the FormView works!! Thanks. But why doesnt it
databind when outside the FormView?

Nov 19 '05 #14
Swetha,

It's because the form view itself is what contains the data. Note your
parameters: <asp:FormView ID="FormView1" runat="server"
DataSourceID="SqlDataSource1" - the datasource is part of the form view and
the form view object iterates through the controls contained within itself
hooking them up to the datasource. If a control is outside the form view it
would get hooked up to the page's datasource (if there was one).

Is that clear? If not please let me know.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Swetha" <sw***********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Putting it inside the FormView works!! Thanks. But why doesnt it
databind when outside the FormView?

Nov 19 '05 #15
Got that!! Thanks for the help and the detailed explanation.

Swetha

Nov 19 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Keith R | last post: by
2 posts views Thread by bill yeager | last post: by
1 post views Thread by Urmal Patel via .NET 247 | last post: by
1 post views Thread by Patrik Zdarsa | last post: by
18 posts views Thread by __PPS__ | last post: by
reply views Thread by zattat | last post: by
1 post views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.