Assign SelectedValue to DropDownList from code-behind file using SqlDataSource

S

Swetha

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("parentIDDropDownList"));

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!
 
S

S. Justin Gengo

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
 
S

Swetha

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
 
S

S. Justin Gengo

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
 
S

Swetha

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("parentIDDropDownList"));

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

S. Justin Gengo

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 said:
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("parentIDDropDownList"));

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

Swetha

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
 
S

S. Justin Gengo

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
 
S

Swetha

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!!
 
S

S. Justin Gengo

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
 
S

Swetha

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

Swetha

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!!
 
S

Swetha

Putting it inside the FormView works!! Thanks. But why doesnt it
databind when outside the FormView?
 
S

S. Justin Gengo

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
 
Joined
Dec 29, 2008
Messages
2
Reaction score
0
The way you bind the data is wrong.

The hidden field should be inside the FormView tags and inside an item template. In your case it was sitting outside. Example:

<asp:FormView ID="FormView1" runat="server" DataKeyNames="ID" CellPadding="4"
ForeColor="#333333">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<ItemTemplate>
<asp:Label ID="PhotosLabel" runat="server" Text='<%# Bind("Photos") %>' Visible="false" />
<br />
<asp:Label ID="UserLabel" runat="server" Text='<%# Bind("User") %>' Visible="false"/>
<br />
</ItemTemplate>
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
</asp:FormView>



radu_m
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top