如何从ASP中选择一个选项:datalist? [英] How to select one option from ASP:datalist?

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

问题描述

需要从网络自定义控件中的datalist中选择一个asp:radiobutton。



> radiobutton select all不选择一个。



>没有想法把它给它。



在此先感谢。



我尝试了什么:



< pre>< asp:DataList ID =    lstView runat =   server  RepeatDirection =  水平 RepeatLayout =    onselectedindexchanged =   lstView_SelectedIndexChanged >  

< ItemTemplate>
< div >
< asp:ImageButton ID = < span class =code-string> btnimage ImageUrl = ../ images / ActivePc.png runat = server CommandName = 点击 />

< asp:RadioButtonList id = rdo runat = server DataTextField = ' <%#Eval(name)%>' />

< asp:标签ID = lbl runat = server Text = ' <%#Eval(name)%>' CssClass = lblcust > < / asp:标签 >


< / div >

< / ItemTemplate >

< / asp:DataList >







< br $> b $ b

 

解决方案





尝试更改RadioButton的组件RadioButtonList。



在C#代码中,到捕获RadioButton,使用下面的代码。



  protected   void  lstView_SelectedIndexChanged( object  sender,EventArgs e)
{
RadioButton rbl =(RadioButton) this .lstView.Items [ this .lstView.SelectedIndex] .FindControl( rdo);
}


将radiobuttonlist更改为radiobutton



< asp:RadioButton id =rdorunat =serverText ='<%#Eval(name)%>'/> 





然后在你的代码隐藏中为你想要的任何一个字段设置rdo的值。这里我正在做第一个



 RadioButton rdo =(RadioButton)lstView.Items [0] .FindControl(rdo); 
rdo.Checked = true;


您好,



解决您的问题,你必须为每个项目分配onCheckedChanged事件。



当你点击一个选项时,事件被触发,你可以操纵你点击的对象。



我为你树立了榜样。您可以复制代码并将其粘贴到项目中并进行测试。代码正在评论以帮助您。



 <   asp:DataList     ID   =  lstView    runat   =  server      RepeatDirection   = 水平    RepeatLayout   =     OnItemCreated   =  lstView_It emCreated     >  
< ItemTemplate >
< div >
< asp:RadioButton id = rdo runat = server AutoPostBack = True 文字 =' <%# DataBinder.Eval(Container.DataItem, name)%>' / >
< asp:标签 ID = lbl runat = server CssC lass = lblcust 文本 =' <%#DataBinder.Eval(Container.DataItem, id) %>' > < / asp:标签 >
< / div >
< / ItemTemplate >
< / asp:DataList & gt;
< asp:Label ID = lblId style = < span class =code-keyword>颜色:红色;字体重量:粗体; font-size:14px; runat = server 文本 = > < / asp:标签 >





您的代码隐藏。完整代码。



< pre lang =c#> public partial class frmMyTests:System.Web.UI.Page
{

// 要加载列表的数据
private DataTable create_dada_source()
{
DataTable dt = new DataTable();
dt.Columns.Add( id typeof string ));
dt.Columns.Add( name typeof string ));

DataRow dr;
for int i = 0 ; i < 9 ; i ++)
{
dr = dt .NewRow();
dr [ id] = Id: + i.ToString();
dr [ name] = 选项: + i.ToString()。PadLeft( 2 ' 0');
dt.Rows.Add(dr);

}

return dt;
}

受保护 void Page_Load( object sender,EventArgs e)
{
if (!IsPostBack)
{
// 加载数据列表
this .lstView.DataSource = this .create_dada_source();
this .DataBind();
}
}

受保护 void rdo_CheckedChanged( object sender,EventArgs e)
{
RadioButton rbl = null ;

// 读取数据列表中的所有项目
foreach (DataListItem lstitem in this .lstView.Items )
{
rbl =(RadioButton)lstitem.FindControl( rdo );
// 比较项目是否与我点击的相同
if (rbl.Text!=((RadioButton)sender).Text)
{
// 所有项目的假属性,我点击的例外
rbl.Checked = false ;
}
其他
{
// < span class =code-comment>用我的id读取标签
this .lblId.Text =((Label)lstitem.FindControl ( lbl))。文字;
}
}
}

// Assigns对radiobutton的事件
protected void lstView_ItemCreated( object sender,DataListItemEventArgs e)
{
// find只有项目,忽略标题
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
// 将事件分配给每个单选按钮项目
((RadioButton)e.Item.FindControl( rdo))。CheckedChanged + = new System.Eve ntHandler(rdo_CheckedChanged);

}
}
}


Need to select an asp:radiobutton from datalist in web custom control.

>radiobutton select all does not select one.

> dont have idea to handel it.

Thanks In Advance.

What I have tried:

<pre><asp:DataList ID="lstView" runat="server"  RepeatDirection="Horizontal" RepeatLayout="Table" onselectedindexchanged="lstView_SelectedIndexChanged"  >
                     
       <ItemTemplate>
<div >
<asp:ImageButton ID="btnimage"      ImageUrl="../images/ActivePc.png"   runat="server"  CommandName="click"    /> 

<asp:RadioButtonList   id="rdo"  runat="server" DataTextField='<%#Eval("name")%>'  />

<asp:Label ID="lbl" runat="server" Text='<%#Eval("name")%>' CssClass="lblcust" ></asp:Label>
                        
         
           </div>

             </ItemTemplate>
                      
               </asp:DataList>






解决方案

Hi,

Try to change the component RadioButtonList for RadioButton.

In the C# code, to capture the RadioButton, use the code below.

protected void lstView_SelectedIndexChanged(object sender, EventArgs e)
{
            RadioButton rbl = (RadioButton)this.lstView.Items[this.lstView.SelectedIndex].FindControl("rdo");
}


Change the radiobuttonlist to a radiobutton

<asp:RadioButton id="rdo"  runat="server" Text='<%#Eval("name")%>' />



Then in your code-behind set the value of the rdo for whichever field you want. Here I'm doing the first one

RadioButton rdo = (RadioButton) lstView.Items[0].FindControl("rdo");
rdo.Checked = true;


Hi,

To resolve your issue, you must assign the onCheckedChanged event to each item.

When you click an option, the event is fired and you can manipulate the object you clicked.

I've set an example for you. You can copy the code and paste it into your project and test it. The code is commenting to help you.

<asp:DataList ID="lstView" runat="server"  RepeatDirection="Horizontal" RepeatLayout="Table" OnItemCreated="lstView_ItemCreated"  >
       <ItemTemplate>
            <div >
                 <asp:RadioButton id="rdo"  runat="server" AutoPostBack="True" Text='<%#DataBinder.Eval(Container.DataItem, "name")%>'  />
                 <asp:Label ID="lbl" runat="server" CssClass="lblcust" Text='<%#DataBinder.Eval(Container.DataItem, "id")%>' ></asp:Label>
           </div>
       </ItemTemplate>
</asp:DataList>
<asp:Label ID="lblId" style="color:red; font-weight:bold; font-size:14px;" runat="server" Text=""></asp:Label>



Your code-behind. Complete Code.

public partial class frmMyTests : System.Web.UI.Page
    {
        
        // Data to load the list
        private DataTable create_dada_source()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("id", typeof(string));
            dt.Columns.Add("name", typeof(string));

            DataRow dr;
            for(int i = 0; i < 9; i++)
            {
                dr = dt.NewRow();
                dr["id"] = "Id: " + i.ToString();
                dr["name"] = "option: " + i.ToString().PadLeft(2, '0');
                dt.Rows.Add(dr);

            }

            return dt;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Load the datalist
                this.lstView.DataSource = this.create_dada_source();
                this.DataBind();
            }
        }

        protected void rdo_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rbl = null;
            
            //Read all items from the datalist
            foreach(DataListItem lstitem in this.lstView.Items)
            {
                rbl = (RadioButton)lstitem.FindControl("rdo");
                //Compare if the item is the same as I clicked
                if (rbl.Text != ((RadioButton)sender).Text)
                {
                    //False attribute for all items, exception that I clicked
                    rbl.Checked = false;
                }
                else
                {
                    //read the label with my id 
                    this.lblId.Text = ((Label)lstitem.FindControl("lbl")).Text;
                }
            }
        }

        //Assigns the event to radiobutton
        protected void lstView_ItemCreated(object sender, DataListItemEventArgs e)
        {
            //find only the item, ignore the header
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                //Assigns the event to each radiobutton item
                ((RadioButton)e.Item.FindControl("rdo")).CheckedChanged += new System.EventHandler(rdo_CheckedChanged);

            }
        }
    }


这篇关于如何从ASP中选择一个选项:datalist?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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