我的gridview标题中有一个radiobuttonlist,如何查找该radiobuttonlist的控件 [英] I have a radiobuttonlist in my gridview header, how can I findcontrol of that radiobuttonlist

查看:100
本文介绍了我的gridview标题中有一个radiobuttonlist,如何查找该radiobuttonlist的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <   asp:TemplateField     HeaderText   = 新排名 >  
< headertemplate >
< asp:标签 ID = lblpos runat = 服务器 > 新职位
< asp:RadioButtonList ID = rblYesNoPos runat = server AutoPostBack = true < span class =code-keyword>>
< asp:ListItem 已选择 = True = >
< asp:ListItem < span class =code-attribute> = >


< ; itemtemplate >
< asp:标签 ID = lblPosTot runat = server 文本 =' <% #Bind( PosTotal)%>' >


< asp:TemplateField HeaderText = 替换 >
< headertemplate >
< asp:标签 ID = lblre runat = server > 替换
< asp: RadioButtonList ID = rblYesNoRe runat = server AutoPostBack = true >
< asp:ListItem 已选择 = True = 文字 = >
< asp:ListItem = 文字 = >


< itemtemplate >
< asp:标签 ID = lblReTot runat = server 文字 =' <% #Bind( ReTotal)%>' >

<





我有什么试过:



 RadioButtonList rbl =(RadioButtonList)e.Row.Cells [ 1 ]。FindControl(  rblYesNoPos); 
RadioButtonList rblR =(RadioButtonList)e.Row.Cells [ 1 ]。FindControl( rblYesNoRe);

解决方案

尝试在行上执行FindControl



(RadioButtonList)e.Row.FindControl(rblYesNoPos); 


< blockquote>这取决于你想要访问它的位置。如果你想要它在 RowDataBound 事件,那么你需要确保在使用之前首先检查相应的 DataControlRowType FindControl 方法。例如:



  protected   void  GridView1_RowDataBound( object  sender,GridViewRowEventArgs e)
{
// 访问标题模板控件
if (e.Row.RowType == DataControlRowType .Header)
{
RadioButtonList rblHeader =(RadioButtonList)e.Row.FindControl( rblYesNoPos );

}

// 访问行模板控件
if (e.Row.RowType == DataControlRowType.DataRow)
{
RadioButtonList rblRow =(RadioButtonList)e.Row .FindControl( rblYesNoRe);

}
}





如果想在按钮引用它们单击事件,其中Button位于 GridView 模板中,然后您可以执行以下操作:



  protected   void  Button1_Click( object  sender,EventArgs e)
{
Button b =(Button)sender;
GridViewRow row =(GridViewRow)b.NamingContainer;
if (row!= null
{
// 访问标题模板控件
if (row.RowType == DataControlRowType.Header)
{
RadioButtonList rblHeader =(RadioButtonList)row.FindControl( rblYesNoPos);

}

// 访问行模板控件
if (row.RowType == DataControlRowType.DataRow)
{
RadioButtonList rblRow =(RadioButtonList)row.FindControl(< span class =code-string>
rblYesNoRe);

}
}
}


<asp:TemplateField HeaderText="New Position">                       
     <headertemplate>
          <asp:Label ID="lblpos" runat="server">New Position
          <asp:RadioButtonList ID="rblYesNoPos" runat="server" AutoPostBack="true">
                <asp:ListItem Selected="True" Value="Yes">Yes
                 <asp:ListItem Value="No">No
          
     
     <itemtemplate>
        <asp:Label ID="lblPosTot" runat="server" Text='<%# Bind("PosTotal")%>'>                            
     

<asp:TemplateField HeaderText="Replacement">
     <headertemplate>
         <asp:Label ID="lblre" runat="server">Replacement
         <asp:RadioButtonList ID="rblYesNoRe" runat="server" AutoPostBack="true">
         <asp:ListItem Selected="True" Value="Yes" Text="Yes">Yes
         <asp:ListItem Value="No" Text="No">No
         
     
     <itemtemplate>
          <asp:Label ID="lblReTot" runat="server" Text='<%# Bind("ReTotal")%>'>
      
<



What I have tried:

RadioButtonList rbl = (RadioButtonList)e.Row.Cells[1].FindControl("rblYesNoPos");
RadioButtonList rblR = (RadioButtonList)e.Row.Cells[1].FindControl("rblYesNoRe");

解决方案

Try doing FindControl on the Row

(RadioButtonList)e.Row.FindControl("rblYesNoPos");


That depends on where do you want to access it. If you want it at RowDataBound event, then you need to make sure you are checking for the appropriate DataControlRowType first before using FindControl method. For example:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //access header template controls
    if (e.Row.RowType == DataControlRowType.Header)
    {
         RadioButtonList rblHeader = (RadioButtonList)e.Row.FindControl("rblYesNoPos");

    }

    //access row template controls
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
         RadioButtonList rblRow = (RadioButtonList)e.Row.FindControl("rblYesNoRe");

    }
} 



If want to reference them at Button Click event where your Button is inside the GridView template, then you can do something like this:

protected void Button1_Click(object sender, EventArgs e)
{
        Button b = (Button)sender;
        GridViewRow row = (GridViewRow)b.NamingContainer;
        if (row != null)
        {
            //access header template controls
            if (row.RowType == DataControlRowType.Header)
            {
                      RadioButtonList rblHeader = (RadioButtonList)row.FindControl("rblYesNoPos");

            }

           //access row template controls
           if (row.RowType == DataControlRowType.DataRow)
           {
                     RadioButtonList rblRow = (RadioButtonList)row.FindControl("rblYesNoRe");

           }
      }
}


这篇关于我的gridview标题中有一个radiobuttonlist,如何查找该radiobuttonlist的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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