以编程方式添加Radio btn ASP.NETGridView [英] Programmatically add Radio btn ASP.NETGridView

查看:63
本文介绍了以编程方式添加Radio btn ASP.NETGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!
有谁知道如何将单选按钮添加到动态网格视图中,这将是互斥的?
在GridView中,单选按钮无法分组,因此不能互斥.也就是说,最终用户能够从GridView中同时选择多个单选按钮."
我发现搜索的解决方案之一是添加一个带有HtmlInputRadioButton的模板字段,该模板字段可以互斥,但只能在普通的网格视图中使用,而在动态网格视图中,互斥不起作用.当我选择一个单选按钮并选择下一个时,不会取消选择第一个.我在某处读到它是因为所有单选按钮都有不同的名称.在下面的类中,如何在InstantiateIn方法中为所有单选按钮赋予相同的名称.
另外,一旦我正确设置了单选按钮...如何确定该行是否已选中了单选按钮?

昨天我问了同样的问题.我再次寻求您的帮助.提前谢谢.

这是我使用的代码:

Hi!
Does anyone know how to add a radiobutton to a dynamic gridview that will be mutually exclusive?
"In the GridView, the radiobuttons cannot be grouped and therefore are not mutually exclusive. That is, an end user is able to select multiple radiobuttons simultaneously from the GridView."
One of the solutions I found searching is to add a template field with a HtmlInputRadioButton witch works as mutually exclusive but only in normal gridviews, in the dynamic ones, the mutually exclusive doesn''t work. When I select one of the radiobuttons and select the next the first one is not deselected. I read somewhere it''s because all radiobuttons have different name. How can I give all the radiobuttons same name in InstantiateIn methode look in my class below.
Also, once I have the radiobutton set up correctly...how can I identify whether that row has the radiobutton checked or not?

I asked the same question yesterday. I ask for your help again. Thanks in advance.

Here is the code I use:

TemplateField rbtnColumn = new TemplateField();            
rbtnColumn.HeaderTemplate = new GridViewTemplate(ListItemType.Header, "Select");   
rbtnColumn.ItemTemplate = new GridViewTemplate(ListItemType.Item, "some data"); 
myGridView.Columns.Add(rbtnColumn);
Here is the GridViewTemplate class I use:
public class GridViewTemplate : System.Web.UI.Page, ITemplate
{
    ListItemType templateType;
    string columnName;
    public GridViewTemplate(ListItemType type, string colname)
    {
        templateType = type;
        columnName = colname;
    }
 public void InstantiateIn(System.Web.UI.Control container)
    {
        HtmlInputRadioButton ckh = new HtmlInputRadioButton();     
        switch (templateType)
        {
          case ListItemType.Item:
                container.Controls.Add(rbt);
                break;
         }
    }
}

推荐答案

马吉德,

如果要为所有这些电台使用相同的名称,则可以使用custom radio button.您只需要知道如何形成小的html标记即可.

还要注意,您的模板控件 no需要从页面类继承.这是示例代码.

Hi Majid,

If you want the same name for all those radio then you can use your custom radio button. You just need to know how to form little html markup.

Also note your template control no need to inherit from page class. Here is the sample code.

public class GridViewTemplate :  ITemplate
{
    ListItemType templateType;
    string columnName;
    public GridViewTemplate(ListItemType type, string colname)
    {
        templateType = type;
        columnName = colname;
    }
    public void InstantiateIn(System.Web.UI.Control container)
    {
        MyRadioButton button = new MyRadioButton();
        button.ClientIDMode = ClientIDMode.Static;
        button.ID = "Radio1";
        button.Name = "Radio1";
        button.Value = "Test";
        switch (templateType)
        {
            case ListItemType.Item:
                container.Controls.Add(button);
                break;
        }
    }
}





public class MyRadioButton : RadioButton
{
    public string Name { get; set; }
    public string Value { get; set; }
    protected override void Render(HtmlTextWriter writer)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<input type='radio' id='" + Name + "' name='" + Name + "' value='" + Value + "'/>");
        writer.Write(sb.ToString());
    }
}



并以与您相同的方式添加它.对于标题,请使用asp.net
中可用的默认标题模板.



And add it in the same way as you did. For the header you use the default header template available in asp.net

TemplateField rbtnColumn = new TemplateField();
 rbtnColumn.ItemTemplate = new GridViewTemplate(ListItemType.Item, "some data");
 GridView1.Columns.Add(rbtnColumn);



祝你好运.

根据您的评论改进了部分
-------------------------------------------

您可以做的是处理gridview的行数据绑定事件,并为单选按钮分配值.示例代码如下.

在gridview标记中,分配一个类似于此 OnRowDataBound="Gridview1_RowDatabound"的处理程序,或者在页面加载中分配GridView1.RowDataBound += new GridViewRowEventHandler(Gridview1_RowDatabound);.一旦分配了处理程序,便在处理程序内部



good luck.

Improved part as per your comment
-------------------------------------------

What you can do is handle the row data bound event of the gridview and assign the value for the radio buttons. Sample code as below.

In the gridview markup assign a handler like this OnRowDataBound="Gridview1_RowDatabound" or else in the page load GridView1.RowDataBound += new GridViewRowEventHandler(Gridview1_RowDatabound);. Once the handler is assigned then inside the handler

protected void Gridview1_RowDatabound(object sender,  GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //Assumed here the column index where the radio button present is 3
        // Change the index as per your column index.
        MyRadioButton btn = (MyRadioButton)e.Row.Cells[3].FindControl("Radio1");
        //Assumed here the customer id is the 0th column
        //change accordingly in your data.
        btn.Value = e.Row.Cells[0].Text;
    }
}



您需要根据数据更改单元格索引.现在,单选按钮的值已分配给客户ID.希望有帮助.



The cell indices you need to change as per your data. Now the radio button''s value are assigned for the customer id. Hope that helps.


这篇关于以编程方式添加Radio btn ASP.NETGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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