选择datalist中的单选按钮 [英] selecting radio button in datalist

查看:66
本文介绍了选择datalist中的单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从数据库中选择了一些数据并通过阅读器显示在

datalist中,如上所示对于单选按钮我有单列返回

1,2,3并根据我想要上面的单选按钮之一

在这个数据主义者中预先选择我怎么能这样做

做这样的事情

但这会给出错误找不到类型或命名空间名称''rdbtn''

(你错过了使用指令或程序集引用吗?)"

作为其数据主义者


< table width =" 100%">

< tr width =" 100%"> ;

< td width =" 25%"><%#DataBinder.Eval(Container.DataItem," FName")%>< / td>

< td width =" 25%">< input type = text name =" txtPass"

value =''<%#DataBinder.Eval( Container.DataItem,Pass wd)%>''>< / td>

< td width =" 45%&q UOT; align =" left">


< input type = hidden name =" txtPerm"

value =''<%#DataBinder。 Eval(Container.DataItem," Perm ission")%>''>

<%string x = Request.Form.Get(" txtPerm");%>

<%if(x ==" 1")

{

rdbtn.SelectedValue =" 1";

}

else if(x ==" 2")

{

rdbtn.SelectedValue =" 2";

}

else if(x ==" 3")

{

rdbtn.SelectedValue =" 3" ;;

}%>

< / td>

< td width =" 5%" align =" left">

< / td>

< / tr>

< / table>

i am selectin some data from database and through reader showing it in
datalist as above now for radio buttons i have single column which returns
1,2,3 and according to that i want one of above radio button to be
preselected in this datalist how can i do that
doing something like this
but this gives error "The type or namespace name ''rdbtn'' could not be found
(are you missing a using directive or an assembly reference?)"
as its in datalist

<table width="100%">
<tr width="100%">
<td width="25%"><%#DataBinder.Eval(Container.DataItem, "FName")%></td>
<td width="25%"><input type=text name="txtPass"
value=''<%#DataBinder.Eval(Container.DataItem,"Pass wd")%>''></td>
<td width="45%" align="left">

<input type=hidden name="txtPerm"
value=''<%#DataBinder.Eval(Container.DataItem,"Perm ission")%>''>
<%string x=Request.Form.Get("txtPerm");%>
<% if(x=="1")
{
rdbtn.SelectedValue="1";
}
else if(x=="2")
{
rdbtn.SelectedValue="2";
}
else if(x=="3")
{
rdbtn.SelectedValue="3";
}%>
</td>
<td width="5%" align="left">
</td>
</tr>
</table>

推荐答案

您好Vikas,


欢迎来到ASPNET新闻组。


从您的描述中,我了解到您正在开发asp.net网页

,它使用datalist控件从数据库中填充一些数据。并且

datalist的模板包含一些radiobutton,radiobutton的数据是通过数据绑定从数据对象填充的
。目前你在预先选择某个单选按钮时遇到了一些问题,根据数据对象的某个字段确定了
。正确吗?


根据我的经验,对于这种情况,您可以考虑使用

辅助函数或ItemDataBound事件来自定义

模板:


例如:


1.我们可以定义一个辅助函数并用它来设置radiobutton'' s

选择状态

< asp:RadioButton ID =" rb1" RUNAT = QUOT;服务器"组名= QUOT; GP1" Checked =''<%#

SetChecked(" rb1",Container.DataItem)%>''/>

< asp:RadioButton ID = " RB2" RUNAT = QUOT;服务器"组名= QUOT; GP1" Checked =''<%#

SetChecked(" rb2",Container.DataItem)%>''/>

======= =代码隐藏页面类中的受保护函数==========

protected bool SetChecked(string rbid,object obj)

{

DataRowView drv = obj as DataRowView;


if(rbid ==" rb1")

{

return ((int)(drv [0])%2 == 0);

}


return!((int)(drv [0]) %2 == 0);


}


2.此外,我们可以使用ItemDataBound将模块中的radioButton控件作为模板并对其进行操作:


protected void DataList1_ItemDataBound(对象发送者,

DataListItemEventArgs e )

{

DataRowView drv = e.Item.DataItem as DataRowView;


RadioButton rb1 =(RadioButton)e.Item .FindControl(" rb1");

RadioButton rb2 =(RadioButton)e.Item.FindControl(" rb2");


//代码设置检查状态....


}

BTW,在您发布的代码片段中,我发现您正在使用内联
数据呈现表达式(<%xxxxx%>),我不认为这是推荐的方法,并且<% %GT;应该在首页模板中使用

,在数据绑定控件的模板中,我们总是建议使用数据绑定表达式(<%) #%>)


希望这会有所帮助。


问候,


Steven Cheng

Microsoft在线社区支持

=============================== ===================


在回复帖子时,请回复群组通过您的新闻阅读器

,其他人可以从您的问题中学习并从中受益。


================ ==================================

此帖子提供按现状 ;没有保证,也没有赋予任何权利。


安全! www.microsoft.com/security

(此帖子按原样提供,不作任何保证,并且不授予

权利。)

Hi Vikas,

Welcome to the ASPNET newsgroup.

From your description, I understand you''re developing asp.net web page
which use a datalist control to populate some datas from database. And the
datalist''s template contains some radiobuttons, the radiobutton''s data is
populated from data object through databinding. Currently you''re
encountering some problem on preselect the certain radio button according
to the data object''s certain field. correct?

Based on my experience, for such scenario, you can consider either using
helper function or the ItemDataBound event to customize the controls in
template:

e.g:

1. we can define a helper function and use it for setting the radiobutton''s
selection status
<asp:RadioButton ID="rb1" runat="server" GroupName="gp1" Checked=''<%#
SetChecked("rb1", Container.DataItem) %>'' />
<asp:RadioButton ID="rb2" runat="server" GroupName="gp1" Checked=''<%#
SetChecked("rb2", Container.DataItem) %>'' />
========a protected function in codebehind page class==========
protected bool SetChecked(string rbid, object obj)
{
DataRowView drv = obj as DataRowView;

if (rbid == "rb1")
{
return ((int)(drv[0]) % 2 == 0);
}

return !((int)(drv[0]) % 2 == 0);

}

2. Also, we can use "ItemDataBound" event to get the radioButton control in
the template and manipulate them:

protected void DataList1_ItemDataBound(object sender,
DataListItemEventArgs e)
{
DataRowView drv = e.Item.DataItem as DataRowView;

RadioButton rb1 = (RadioButton)e.Item.FindControl("rb1");
RadioButton rb2 = (RadioButton)e.Item.FindControl("rb2");

// code to set check status....

}
BTW, in the code snippetd you posted, I found that you''re using the inline
data rendering expression( <% xxxxx %> ) in the DataList''s template, I
don''t think this is the recommended means, and <% %> is supposed to be used
in the top page template, and in databound control''s template, we''re always
recommended to use databinding expression( <%# %>)

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


您好,谢谢回复是你的问题是正确的

i想要使用

ItemDataBound事件来定制控件

模板


i的编码是这样的


< ItemTemplate>

< table width =" 100%">

< tr width =" 100%">

< td

width =" 25%"><%#DataBinder.Eval (Container.DataItem," FName")%>< / td>

< td width =" 25%">< input type = text name =" txtPass" ;

value =''<%#DataBinder.Eval(Container.DataItem," Pass wd")%>''>< / td>

< td width =" 45%" align =" left">

< asp:RadioButton ID =" rb1" RUNAT = QUOT;服务器" GroupName =" gp1"

/>& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& ; nbsp;& nbsp;

< asp:RadioButton ID =" rb2" RUNAT = QUOT;服务器" GroupName =" gp1"

/>& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& ; nbsp;& nbsp;& nbsp;

< asp:RadioButton ID =" rb3" RUNAT = QUOT;服务器" GroupName =" gp1"

/>


< / td>

< td width =" 5 %QUOT; align =" left">

< asp:Button ID =" EditSubuser" Text =" Edit"

Runat =" server">< / asp:Button>< / td>

< / tr>

< / table>

< / ItemTemplate>


和代码背后

protected void listUser_ItemDataBound (对象发送者,

System.Web.UI.WebControls.DataListItemEventArgs e)


{


DataRowView drv = e.Item.DataItem as DataRowView;


RadioButton rb1 =(RadioButton)e.Item.FindControl(" rb1");


RadioButton rb2 =(RadioButton)e.Item.FindControl(" rb2");


RadioButton rb3 =(RadioButton)e.Item.FindControl(" rb3"); < br $>
}


它的工作正常,直到这里

但我现在如何选择这三种中的一种

下面的代码从权限colomn中选择1,2或3

如何从权限coloumn取值并将设置其中一个

三rd1或rd2或rd3


str ing strsql =" select

Userinfo.UID,Userinfo.FName,Userinfo.Permission,Us erPassword.Passwd from

Userinfo,UserPassword,其中Userinfo.UID = UserPassword.UID和$>
Userinfo.ParentID =" + userid;


数据库db = DatabaseFactory.CreateDatabase();


DBCommandWrapper cmd = db.GetSqlStringCommandWrapper(strsql);


dr = db.ExecuteReader(cmd);


listUser.DataSource = dr;


listUser.DataBind();


dr.Close();


" Steven Cheng [MSFT]" < ST ***** @ online.microsoft.com>写在消息

新闻:lU ************** @ TK2MSFTNGXA01.phx.gbl ...
Hi thanks for reply yes u got the problem correct
i want to use
ItemDataBound event to customize the controls in
template

i had coded like this

<ItemTemplate>
<table width="100%">
<tr width="100%">
<td
width="25%"><%#DataBinder.Eval(Container.DataItem, "FName")%></td>
<td width="25%"><input type=text name="txtPass"
value=''<%#DataBinder.Eval(Container.DataItem,"Pass wd")%>''></td>
<td width="45%" align="left">
<asp:RadioButton ID="rb1" runat="server" GroupName="gp1"
/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;
<asp:RadioButton ID="rb2" runat="server" GroupName="gp1"
/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;
<asp:RadioButton ID="rb3" Runat="server" GroupName="gp1"
/>

</td>
<td width="5%" align="left">
<asp:Button ID="EditSubuser" Text="Edit"
Runat="server"></asp:Button></td>
</tr>
</table>
</ItemTemplate>

and in code behind
protected void listUser_ItemDataBound(object sender,
System.Web.UI.WebControls.DataListItemEventArgs e)

{

DataRowView drv = e.Item.DataItem as DataRowView;

RadioButton rb1 = (RadioButton)e.Item.FindControl("rb1");

RadioButton rb2 = (RadioButton)e.Item.FindControl("rb2");

RadioButton rb3=(RadioButton)e.Item.FindControl("rb3");
}

its working fine till here
but how will i select one of these three now
the code below selects 1,2 or 3 from permission colomn
how will i take value from permission coloumn and will set one of these
three rd1 or rd2 or rd3

string strsql="select
Userinfo.UID,Userinfo.FName,Userinfo.Permission,Us erPassword.Passwd from
Userinfo,UserPassword where Userinfo.UID=UserPassword.UID and
Userinfo.ParentID="+ userid;

Database db=DatabaseFactory.CreateDatabase();

DBCommandWrapper cmd=db.GetSqlStringCommandWrapper(strsql);

dr=db.ExecuteReader(cmd);

listUser.DataSource=dr;

listUser.DataBind();

dr.Close();

"Steven Cheng[MSFT]" <st*****@online.microsoft.com> wrote in message
news:lU**************@TK2MSFTNGXA01.phx.gbl...
嗨Vikas,

欢迎来到ASPNET新闻组。

从您的描述中,我了解到您正在开发asp.net网页
使用datalist控件来填充一些数据来自数据库。并且
datalist的模板包含一些radiobutton,通过数据绑定从数据对象填充radiobutton的数据。目前你在根据数据对象的某个字段预选某个单选按钮时遇到了一些问题。正确吗?

根据我的经验,对于这种情况,您可以考虑使用
辅助函数或ItemDataBound事件来自定义
模板中的控件:
<例如:

1.我们可以定义一个辅助函数并用它来设置
radiobutton'的选择状态

< ; asp:RadioButton ID =" rb1" RUNAT = QUOT;服务器"组名= QUOT; GP1" Checked =''<%#
SetChecked(" rb1",Container.DataItem)%>''/>
< asp:RadioButton ID =" rb2" RUNAT = QUOT;服务器"组名= QUOT; GP1" Checked =''<%#
SetChecked(" rb2",Container.DataItem)%>''/>

========受保护的功能在codebehind页面类中==========
protected bool SetChecked(string rbid,object obj)
{DataRowView drv = obj as DataRowView;

if(rbid ==" rb1")
{
return((int)(drv [0])%2 == 0);
}

return!((int)(drv [0])%2 == 0);

}

2.此外,我们可以使用ItemDataBound。在模板中获取radioButton控件的事件并操作它们:

protected void DataList1_ItemDataBound(object sender,
DataListItemEventArgs e)
{
DataRowView drv = e.Item.DataItem as DataRowView;

RadioButton rb1 =(RadioButton)e.Item.FindControl(" rb1");
RadioButton rb2 =(RadioButton)e .Item.FindControl(rb2);

//代码设置检查状态....

}

BTW,in您发布的代码片段,我发现您在DataList的模板中使用内联
数据呈现表达式(<%xxxxx%>),我不认为这是推荐的方法,< %%>应该在首页模板中使用

在数据绑定控件的模板中,我们总是建议使用数据绑定表达式(<% #%>)
希望这会有所帮助。

问候,

Steven Cheng
微软在线社区支持

=============================================== ===

在回复帖子时,请回复群组通过您的新闻阅读器,以便其他人可以从您的问题中学习并从中受益。

======================== ==========================

此帖子按原样提供。没有保证,也没有授予
权利。

获得安全! www.microsoft.com/security
(此帖已提供按原样,没有任何保证,也没有赋予
权利。)
Hi Vikas,

Welcome to the ASPNET newsgroup.

From your description, I understand you''re developing asp.net web page
which use a datalist control to populate some datas from database. And the
datalist''s template contains some radiobuttons, the radiobutton''s data is
populated from data object through databinding. Currently you''re
encountering some problem on preselect the certain radio button according
to the data object''s certain field. correct?

Based on my experience, for such scenario, you can consider either using
helper function or the ItemDataBound event to customize the controls in
template:

e.g:

1. we can define a helper function and use it for setting the
radiobutton''s
selection status
<asp:RadioButton ID="rb1" runat="server" GroupName="gp1" Checked=''<%#
SetChecked("rb1", Container.DataItem) %>'' />
<asp:RadioButton ID="rb2" runat="server" GroupName="gp1" Checked=''<%#
SetChecked("rb2", Container.DataItem) %>'' />
========a protected function in codebehind page class==========
protected bool SetChecked(string rbid, object obj)
{
DataRowView drv = obj as DataRowView;

if (rbid == "rb1")
{
return ((int)(drv[0]) % 2 == 0);
}

return !((int)(drv[0]) % 2 == 0);

}

2. Also, we can use "ItemDataBound" event to get the radioButton control
in
the template and manipulate them:

protected void DataList1_ItemDataBound(object sender,
DataListItemEventArgs e)
{
DataRowView drv = e.Item.DataItem as DataRowView;

RadioButton rb1 = (RadioButton)e.Item.FindControl("rb1");
RadioButton rb2 = (RadioButton)e.Item.FindControl("rb2");

// code to set check status....

}
BTW, in the code snippetd you posted, I found that you''re using the inline
data rendering expression( <% xxxxx %> ) in the DataList''s template, I
don''t think this is the recommended means, and <% %> is supposed to be
used
in the top page template, and in databound control''s template, we''re
always
recommended to use databinding expression( <%# %>)

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)



感谢您的回复Vikas,

既然你已经在ItemDataBound事件中获得了DataRowView对象,那么你可以查询某个列(通过数据库中的dataReader选择)值

来自它,例如:


=================================

protected void listUser_ItemDataBound(object sender,

System.Web.UI.WebControls.DataListItemEventArgs e)


{


DataRowView drv = e.Item.DataItem as DataRowView;

int selval =(int)drv [" selectvalue"];


RadioButton rb1 =(RadioButton)e.Item.FindControl(" rb1");


RadioButton rb2 =(RadioButton)e.Item.FindControl(" rb2") ;
/>
RadioButton rb3 =(RadioButton)e.Item.FindControl(" rb3");

}

========= ===========

DataRowView有点像DataRow,你可以通过索引访问那些数据库

列'的值或者名称:


#DataRowView类
http://msdn2.microsoft.com/en-us/lib...ew(VS.80).aspx


然后,您可以根据列的值将某些RadioButton的Checked属性设置为false或true




希望这会有所帮助。如果您还有任何不清楚的地方,请随时在这里发布




问候,


Steven Cheng

Microsoft在线社区支持

============================== ====================


在回复帖子时,请回复群组通过您的新闻阅读器

,其他人可以从您的问题中学习并从中受益。


================ ==================================

此帖子提供按现状 ;没有保证,也没有赋予任何权利。


安全! www.microsoft.com/security

(此帖子按原样提供,不作任何保证,并且不授予

权利。)




Thanks for your response Vikas,

Since you''ve got the DataRowView object in ItemDataBound event, then you
can query the certain column(select through dataReader in database) value
from it, e.g:

=================================
protected void listUser_ItemDataBound(object sender,
System.Web.UI.WebControls.DataListItemEventArgs e)

{

DataRowView drv = e.Item.DataItem as DataRowView;
int selval = (int)drv["selectvalue"];

RadioButton rb1 = (RadioButton)e.Item.FindControl("rb1");

RadioButton rb2 = (RadioButton)e.Item.FindControl("rb2");

RadioButton rb3=(RadioButton)e.Item.FindControl("rb3");
}
====================

DataRowView is just somewhat like DataRow, you can access those database
column''s value through index or name:

#DataRowView Class
http://msdn2.microsoft.com/en-us/lib...ew(VS.80).aspx

Then, you can set certain RadioButton''s Checked property to false or true
based on the column''s value.

Hope this helps. If you still have anything unclear, please feel free to
post here.

Regards,

Steven Cheng
Microsoft Online Community Support
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)





这篇关于选择datalist中的单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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