在gridview中需要帮助radiobutton

时间:2009-07-22 14:37:57

标签: asp.net gridview radio-button

通过单选按钮我从gridview中选择数据。这里所有单选按钮一次可选。实际上它不应该发生在用户只能选择一个我不能做的按钮的时候。我的第二个问题是当我选择特定的单选按钮时,应在文本框中显示细节。我在gridview中有Itemid,ItemName,Quantity,Rate和Total字段。这些值我通过文本框插入,所以我有所有相应的文本框。因此,一旦我选择特定的radiobutton,这些细节应显示在相应的文本框中。我已经为此完成了插入编码,但无法通过文本框选择通过radiobutton和dispalying。请有人帮我编码解决这个问题。

谢谢, SUMIT

3 个答案:

答案 0 :(得分:2)

萨米特, 不要使用html控件,使用asp控件:

<asp:RadioButton ID="RadioSelector" runat="server" GroupName="RadioSelectors" />

我在ASP.NET类中遇到了类似的问题,并且我遵循了this教程,该教程运行良好。

答案 1 :(得分:1)

听起来像经典的主/细节模式,请看:

Tutorial 10: Master/Detail Using a Selectable Master GridView with a Details DetailView

您正在使用单选按钮来对抗ASP.NET数据绑定控件的预期工作。我不喜欢选择链接,要么它们不完全是Web 2.0!但是通过执行此操作(或其变体)可以很容易地用行单击替换它们:

Select a row in an asp:GridView without using a Select Command

答案 2 :(得分:0)

我在网上看过几篇文章但没有一篇是合适的。我终于想出了我自己的解决方案,没有使用HTMLControls radiobutton也没有使用Javascript。这符合我的要求。


我的Gridview html设置如下

    <asp:GridView ID="grdVersion" runat="server" 
        AutoGenerateColumns="false" AllowPaging="true"
        AutoGenerateEditButton="false" PageSize="10" Width="400px" 
        EmptyDataText="No records available."
        OnRowDataBound="grdVersion_RowDataBound"
        AutoGenerateSelectButton="false">
        <Columns>
            <asp:BoundField DataField="versionid" HeaderText="Version No." ItemStyle-Width="50px"
                ItemStyle-Wrap="false" HtmlEncode="true" ReadOnly="true" />
            <asp:BoundField DataField="version_date" HeaderText="Version Date" ItemStyle-Width="100px"
                ItemStyle-Wrap="false" HtmlEncode="true" ReadOnly="true" />
            <asp:BoundField DataField="remarks" HeaderText="Remarks" ItemStyle-Width="150px"
                ItemStyle-Wrap="true" HtmlEncode="true" ReadOnly="true" />

            **<asp:TemplateField HeaderText="Admin" HeaderStyle-Width="100px">
                <ItemTemplate>
                    <asp:RadioButton ID="rdCurrent" runat="server" 
                    Checked="false" Enabled="true" GroupName="rgVersion" 
                    AutoPostBack="true" 
                    OnCheckedChanged="rdCurrent_CheckChanged" />
                </ItemTemplate>**

             </asp:TemplateField>
        </Columns>
    </asp:GridView>

服务器代码(C#)如下,

   DataTable dtDataSpaceVersions; //place this inside the codebehind page class
   protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            dtDataSpaceVersions = ListDataSpaceVersions();
            ViewState["dtDataSpaceVersions"] = dtDataSpaceVersions;

            PopulateGridVersion();
        }
    }

    protected void PopulateGridVersion()
    {
        grdVersion.DataSource = dtDataSpaceVersions;
        grdVersion.DataBind();
    }



    protected void rdCurrent_CheckChanged(object sender, EventArgs e)
    {

        Control selectedVersion = ((Control)sender).Parent;

        if (ViewState["dtDataSpaceVersions"] != null)
            dtDataSpaceVersions = (DataTable)ViewState["dtDataSpaceVersions"];

        foreach (DataRow dtr in dtDataSpaceVersions.Rows)
        {
            if (dtr["versionid"].ToString() == ((System.Web.UI.WebControls.GridViewRow)selectedVersion.Parent).Cells[0].Text)
                dtr[3] = "Y";
            else
                dtr[3] = "N";
        }
        PopulateGridVersion();
    }


    protected void grdVersion_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        DataRowView drv;
        if (e.Row.RowType == DataControlRowType.DataRow && e.Row.DataItem != null)
        {
            drv = (DataRowView)e.Row.DataItem;
            if ((RadioButton)(e.Row.FindControl("rdCurrent")) != null)
                if (drv.Row.ItemArray[3].ToString() == YesNo.N.ToString())
                    ((RadioButton)(e.Row.FindControl("rdCurrent"))).Checked = false;
                else
                    ((RadioButton)(e.Row.FindControl("rdCurrent"))).Checked = true;

            //setGridUserPermissionCheckBoxState(e.Row, drv);
        }
    }


   public DataTable ListDataSpaceVersions()
    {
        string sql = string.Empty;
        DataTable dt = new DataTable();
        dt.Columns.Add("versionid", typeof(String));
        dt.Columns.Add("version_date", typeof(String));
        dt.Columns.Add("remarks", typeof(String));
        dt.Columns.Add("is_current", typeof(String));

        DataRow dtr;
        dtr = dt.NewRow();
        dtr[0] = "1.1";
        dtr[1] = "12-Dec-2005";
        dtr[2] = "Campaign Information";
        dtr[3] = "N";
        dt.Rows.Add(dtr);

        dtr = dt.NewRow();
        dtr[0] = "1.2";
        dtr[1] = "06-Mar-2006";
        dtr[2] = "Sales corrections";
        dtr[3] = "N";
        dt.Rows.Add(dtr);

        dtr = dt.NewRow();
        dtr[0] = "1.3";
        dtr[1] = "24-Aug-2009";
        dtr[2] = "Invoice reconciliation";
        dtr[3] = "Y";
        dt.Rows.Add(dtr);

        dtr = dt.NewRow();
        dtr[0] = "1.4";
        dtr[1] = "30-May-2010";
        dtr[2] = "Invoices verification";
        dtr[3] = "N";
        //dtr[0][0] = "";

        dt.Rows.Add(dtr);
        return dt;
    }