需要 gridview 中单选按钮的帮助

发布于 2024-07-27 21:26:01 字数 335 浏览 4 评论 0原文

通过单选按钮,我从 gridview 选择数据。 这里所有单选按钮都可以一次选择。 实际上,这种情况不应该发生在用户只能选择一个按钮的情况下,而我却无法做到这一点。 我的第二个问题是当我选择特定的单选按钮时,详细信息应显示在文本框中。 我在 gridview 中有 Itemid、ItemName、Quantity、Rate 和 Total 字段。 这些值是我通过文本框插入的,因此我拥有所有相应的文本框。 因此,一旦我选择特定的单选按钮,这些详细信息应该显示在相应的文本框中。 我已经为此完成了插入编码,但无法通过单选按钮进行选择并在文本框中显示。 请有人帮助我编码解决这个问题。

谢谢, 苏米特

Thru radio button i am selecting the data from gridview. Here all radio buttons are selectable at a time. Actually it should not happen at a time user can select only one button which i could not do. My second problem is when i am selecting particular radiobutton that details should be displayed in text box. I have Itemid, ItemName, Quantity, Rate and Total field in gridview. These values i have inserted thru textbox so i have all the corresponding text box for all. So once i select particular radiobutton those details should be displayed in corresponding textbox. I have done the insertion coding for this but couldn't do selecting thru radiobutton and dispalying in textbox. Pls somebody help me in coding for this problem.

Thanks,
sumit

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

热情消退 2024-08-03 21:26:01

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

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

我在 ASP.NET 类中遇到了类似的问题,我遵循 这个教程效果很好。

Sumit,
Don't use the html control, use the asp control:

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

I had a similar problem in an ASP.NET class, and I followed this tutorial which worked perfectly.

桜花祭 2024-08-03 21:26:01

听起来像是经典的主/详细模式,请参见此处:

教程 10:主/详细信息使用带有详细信息 DetailView 的可选主 GridView

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

在 asp:GridView 中选择一行而不使用选择命令

Sounds like the classic master/detail pattern see here:

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

You are fighting the intended workings of ASP.NET databound controls by using radio buttons. I don't like having select links either they're not exactly Web 2.0! but they can be quite easily replaced with a row click by doing this (or variation of same):

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

挖个坑埋了你 2024-08-03 21:26:01

我在网上读了几篇文章,但没有一篇是合适的。 我终于找到了自己的解决方案,没有使用 HTMLControls 单选按钮,也没有使用 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;
    }

I read several articles on the net but none were suitable. I finally figured out my own solution without using either HTMLControls radiobutton nor using Javascript. This works for my requirement.


My Gridview html settings were as follows

    <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>

The server code (C#) was as follows,

   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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文