使用SqlDataSource从代码隐藏文件将SelectedValue分配给DropDownList [英] Assign SelectedValue to DropDownList from code-behind file using SqlDataSource

查看:47
本文介绍了使用SqlDataSource从代码隐藏文件将SelectedValue分配给DropDownList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好


我有一个DropDownList,我使用以下内容填充

SqlDataSource:


< ; asp:DropDownList ID =" parentIDDropDownList" runat =" server"

DataSourceID =" SqlDataSource3" DataTextField =" name"

DataValueField =" ID">< / asp:DropDownList>< asp:SqlDataSource

ID =" SqlDataSource3" RUNAT = QUOT;服务器" ConnectionString ="<%$

ConnectionStrings:ConnectionString%>"

SelectCommand =" SELECT [ID],[name] FROM [client]

ORDER BY [name]">< / asp:SqlDataSource>


在为此DropDownList分配SelectedValue之前,我需要添加

另一个项目。这是我在FormView的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"));

//希望在这里指定myDdl.SelectedValue

// myDdl.SelectedValue = SqlDataSource3。????

}

}

}


插入选择父级后项目,我希望将DropDownList的

SelectedValue分配给SqlDataSource3返回的相应值

。我怎么能这样做?


谢谢!

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!

推荐答案



ConnectionStrings:ConnectionString%>"

SelectCommand =" SELECT [ID],[name] FROM [client]

ORDER BY [name]" >< / asp:SqlDataSource>


在为此DropDownList分配SelectedValue之前,我需要向其添加

另一个项目。这是我在FormView的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"));

//希望在这里指定myDdl.SelectedValue

// myDdl.SelectedValue = SqlDataSource3。????

}

}

}


插入选择父级后项目,我希望将DropDownList的

SelectedValue分配给SqlDataSource3返回的相应值

。我怎么能这样做?


谢谢!


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!


Swetha,


只需将数据源返回的值转换为字符串并分配即可。

SelectedValue将所选值作为字符串获取或设置。


myDdl .SelectedValue = dataSourceValue;


-

此致,


S. Justin Gengo,MCP

网站开发人员/程序员

www.aboutfortunate.com


出于混乱而来的订单。

Nietzsche

" Swetha" < SW *********** @ gmail.com>在消息中写道

news:11 ********************** @ g43g2000cwa.googlegr oups.com ...
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...
你好

我有一个DropDownList,我使用下面的源代码填充了SqlDataSource:

< asp:DropDownList ID =" parentIDDropDownList" runat =" server"
DataSourceID =" SqlDataSource3" DataTextField =" name"
DataValueField =" ID">< / asp:DropDownList>< asp:SqlDataSource
ID =" SqlDataSource3" RUNAT = QUOT;服务器" ConnectionString ="<%
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>

在为此DropDownList分配SelectedValue之前,我需要添加
另一个Item。这是我在FormView的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"));
//希望分配myDdl.SelectedValue这里
// myDdl.SelectedValue = SqlDataSource3。????
}
}

插入选择一个之后亲本QUOT;项目,我希望将DropDownList的
SelectedValue分配给SqlDataSource3返回的相应值。我该怎么做?

谢谢!

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!



这篇关于使用SqlDataSource从代码隐藏文件将SelectedValue分配给DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆