ASP.NET 2.0中的GridView有条件的单选按钮 [英] ASP.NET 2.0 with conditional radio button in a GridView

查看:73
本文介绍了ASP.NET 2.0中的GridView有条件的单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待有一个GridView显示数据库中的事件,然后在最后一栏我想要做的另一个数据库查询(每行),看看是否该事件是否已满。

如果它不饱满,显示一个单选按钮。如果是完全显示文本完全。

我的认为的最好和OnRowDataBound事件做,但可以使用一些帮助。

我可以看到如何改变一个asp标签文本,而不是显示一个单选按钮。

register.aspx:

 < ASP:GridView控件ID =GridView2=服务器的DataSourceID =SqlDataSource2的AutoGenerateColumns =假的cellpadding =5HeaderStyle-背景色=深灰WIDTH = 450像素OnRowDataBound =GridView2_OnRowDataBound>
            <柱体和GT;
                < ASP:BoundField的数据字段=SESSIONDATE的HeaderText =日期DataFormatString ={0:D}/>
                < ASP:BoundField的数据字段=sessionTime的HeaderText =时代周刊/>
                < ASP:BoundField的数据字段=地盘的HeaderText =网站/>
                < ASP:BoundField的数据字段=房间的HeaderText =室DataFormatString ={0:D}/>
                < ASP:的TemplateField的HeaderText =>
                    <&ItemTemplate中GT;
                        <输入类型=电台NAME =rdoSessionID值='<%#的eval(ses_ID)%>' />                    < / ItemTemplate中>
                < / ASP:的TemplateField>
            < /专栏>
            < HeaderStyle背景色=#99CCFF/>
        < / ASP:GridView的>
        < BR />
        < ASP:SqlDataSource的ID =SqlDataSource2=服务器的ConnectionString =<%$的ConnectionStrings:sqlConnection2%>中
            的SelectCommand =SELECT * FROM dbo.sessions WHERE网站,如@ses_site和SESSIONDATE = @ses_date ORDER BY SESSIONDATE>            < SelectParameters>
             < ASP:SessionParameter NAME =ses_siteSessionField =ses_site类型=字符串/>
             < ASP:SessionParameter NAME =ses_dateSessionField =ses_date类型=字符串/>
            < / SelectParameters>        < / ASP:SqlDataSource的>


解决方案

有你可以做到这一点的一些方法。


  1. 您可以做到这一点的OnRowDataBound,如果你正在处理的项目是DataItem的,抓住你从当前行所需要的价值,做一个快速的查找DB

  2. 当你得到你的结果集从数据库返回的,做的每个事件的另一个查找。这样,当您的数据离开你的数据层/方法你已经拥有你需要的一切。

  3. 修改您的SQL查询返回的事件列表信息。你已经知道的事件,你可以只是有一个子查询的查询,看看它的全部或没有(或注册人#和#最大允许这样你就可以证明)。这样,你只需要做对数据库1命中和它会为你节省了一堆的处理。 (我的最爱)

您仍然需要过载这些解决方案的所有3 OnRowDataBound事件。你隐藏的单选按钮,如果满了,并确保它是可见的,如果该事件是不是满了。所不同的是,你在哪里得到你的数据。

你想做的事:

1命中好(事件列表)
小命中的X-金额为每个事件

1命中好事件的列表+如果每一个事件是否已满。

如果您想招待#3,发表您的SQL查询,我们可以从那里。

在OnRowDataBound事件的快速例子。

 保护无效MyGridView_OnRowDataBound(对象发件人,GridViewRowEventArgs E)
{
    如果(e.Row.RowType == DataControlRowType.DataRow)
    {
        复选框CBOX =(复选框)e.Row.FindControl(chkBox);        //做一些时髦的逻辑
        cbox.Visible = Event.HasRoom; //布尔属性格式
        // 要么
        cbox.Visible = Convert.ToBoolean(DataBinder.Eval的(e.Row.DataItemHasRoom)的ToString());    }
}

更新1:

有关你的GridView,您可以使用

 <&ItemTemplate中GT;
   < ASP:复选框=服务器ID =Checkbox1工具提示=注册事件文本=事件东西/>
< / ItemTemplate中>

如果你想使用的模板,不使用常规的控制,使用控制。

有关查询,你可以做到以下几点。我不是你的表结构100%肯定,所以我做的方式我通常会:

表:会话(sess_id(PK),SESSIONDATE,SessionTime,网站,室)
表:注册者(RegistrantID(PK),sess_id(FK到会议),用户ID(FK到已注册用户)

  SELECT SESSIONDATE,SessionTime,网站,室,ses_ID,
(SELECT COUNT(ses_ID)注册人R其中R.Ses_ID = S.ses_Id)为[注册人]
从dbo.Sessions小号

I'm looking to have a GridView displaying "Events" from a database, and then in the last column I want to do another database lookup (for each row) to see if that event is full or not.

If it's not full, display a radio button. If it is full display text "Full".

I think this is best done with OnRowDataBound event but could use a little help.

I can see how to change an asp label text but not display a radio button.

register.aspx:

<asp:GridView ID="GridView2" runat="server" DataSourceID="SqlDataSource2" AutoGenerateColumns=false CellPadding="5" HeaderStyle-BackColor="DarkGray" Width="450px" OnRowDataBound="GridView2_OnRowDataBound">
            <Columns>
                <asp:BoundField DataField="sessionDate" HeaderText="Date" DataFormatString="{0:D}" />
                <asp:BoundField DataField="sessionTime" HeaderText="Time" />
                <asp:BoundField DataField="site" HeaderText="Site" />
                <asp:BoundField DataField="room" HeaderText="Room" DataFormatString="{0:D}" />
                <asp:TemplateField HeaderText="">
                    <ItemTemplate>
                        <input type="radio" name="rdoSessionID" value='<%# Eval("ses_ID") %>' />



                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <HeaderStyle BackColor="#99CCFF" />
        </asp:GridView>
        <br />


        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:sqlConnection2 %>"
            SelectCommand="SELECT * FROM dbo.sessions WHERE site LIKE @ses_site AND sessionDate = @ses_date ORDER BY sessionDate">

            <SelectParameters>
             <asp:SessionParameter name="ses_site" SessionField="ses_site" Type="String" />
             <asp:SessionParameter name="ses_date" SessionField="ses_date" Type="String" />
            </SelectParameters>



        </asp:SqlDataSource>

解决方案

There are a few ways you can do this.

  1. You can do this on the OnRowDataBound , If the Item you're dealing with is a DataItem, grab the value you need from the current row, and do a quick DB Lookup
  2. When you get your resultset back from the database, do another lookup on each event. This way when your data leaves your data layer / methods you already have everything you need.
  3. Modify your SQL query to return this information with the list of events. You already know the Events, you could just have a sub query to query to see if it's full or not (or the # of people registered, and the Max # allowed so you can show that). This way you only have to do 1 hit against the database and it will save you a bunch of processing. (My favorite)

You would still need to overload the OnRowDataBound event for all 3 of those solutions. You'd hide the radio button if it was full, and ensure it was visible if the event was not full. The difference is where do you get your data.

Do you want to do:

1 Good hit (list of events) X amounts of small hits for each event

or

1 Good hit for list of events + if each event is full or not.

If you want to entertain #3, post your SQL Query and we can go from there.

Quick example of the OnRowDataBound event.

protected void MyGridView_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        CheckBox cbox = (CheckBox)e.Row.FindControl("chkBox");

        // Do some funky logic
        cbox.Visible = Event.HasRoom; //Boolean Propery
        // Or
        cbox.Visible = Convert.ToBoolean(DataBinder.Eval(e.Row.DataItem, "HasRoom").ToString());

    }
}

Update 1:

For your GridView, You can use

<ItemTemplate> 
   <asp:CheckBox runat="server" ID="Checkbox1" ToolTip=" Sign up For event " text="Event stuff" /> 
</ItemTemplate>

If you want to use a template, don't use a regular control, use a control.

For the query you could do the following. I'm not 100% sure on your table structure so I did it the way I normally would:

Table: Session (sess_id(PK), SessionDate, SessionTime, Site, Room) Table: Registrants (RegistrantID (PK), sess_id (FK to Session), UserID (FK to users that are registered)

SELECT SessionDate, SessionTime, Site, Room, ses_ID, 
(SELECT Count(ses_ID) FROM Registrants R WHERE R.Ses_ID= S.ses_Id) as [Registrants]
FROM dbo.Sessions s

这篇关于ASP.NET 2.0中的GridView有条件的单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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