gridView中的checkBox

时间:2011-04-13 16:50:18

标签: c#

我的以下代码无法正常运行...代码是关于当用户选中带有网格视图的复选框时,此信息应该触发到表中,并且一旦数据发送到数据库并且两个视图将根据条件创建,如果选中复选框,则视图的结果应显示在标签框中。(在ASP.NET中使用C#和DB-MSSQL)

CREATE TABLE yourBaseTable
  (
   ID INT IDENTITY(1, 1)
       PRIMARY KEY ,
   Payload NVARCHAR(255) NOT NULL ,
   Discriminator TINYINT NOT NULL
              CHECK ( Discriminator IN ( 0, 1 ) )
              DEFAULT ( 0 )
  ) ;
GO

CREATE VIEW yourView1
AS
  SELECT ID ,
      Payload ,
      Discriminator
  FROM  yourBaseTable
  WHERE  Discriminator = 0 ;
GO

CREATE VIEW yourView2
AS
  SELECT ID ,
      Payload ,
      Discriminator
  FROM  yourBaseTable
  WHERE  Discriminator = 1 ;
GO

我也讨厌前端问题

以下是方法'checkBox_checkedChange'的描述..我不想得到一行代码

单击CheckBox会触发CheckBox_CheckedChanged事件。我们需要实现此方法以捕获检查CheckBox的行。

protected void CheckBox1_CheckedChanged(object sender, EventArgs e) {

ClearCheckBoxes();

CheckBox checkbox = (CheckBox)sender;

checkbox.Checked = true;

GridViewRow row = (GridViewRow)checkbox.NamingContainer; // WHAT DOES THIS CODE MEANS ..

DisplayMessage(row.Cells[1].Text);

}

2 个答案:

答案 0 :(得分:2)

我不确定你在第一部分中究竟在问什么。至于底部的问题:

GridViewRow row = (GridViewRow)checkbox.NamingContainer;//WHAT DOES THIS CODE MEANS ..

“.NamingContainer”将获取包含复选框的对象,在这种情况下,您的复选框位于GridViewRow中。您仍然必须将对象强制转换为GridViewRow,这是“(GridViewRow)”的目的。您将它分配给局部变量行,以便您可以继续使用它。

答案 1 :(得分:0)

好的,我从未通过SQL处理过视图。我通过查询处理视图。以下是我将如何处理这个问题的示例。我认为它符合您的要求。这是我的完整代码。你应该能够复制粘贴以查看它运行,除了我使用linq to sql。您可能需要将我的查询转换为SQL,但我强烈建议您使用Linq-to-SQL。它太容易使用了。

aspx页面:

<form id="form1" runat="server">
<div>
    <asp:GridView ID="grdSelected" runat="server" AutoGenerateColumns="False" EnableModelValidation="True" HeaderStyle-BackColor="#1B940A">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Label ID="lblCheckedID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="chkSelected" runat="server" Checked='<%# Bind("Discriminator") %>'
                        OnCheckedChanged="chkSelected_CheckedChanged" AutoPostBack="true" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Payload") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

    <asp:GridView ID="grdNotSelected" runat="server" AutoGenerateColumns="False" EnableModelValidation="True" HeaderStyle-BackColor="#84090C">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Label ID="lblCheckedId" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="chkNotSelected" runat="server" Checked='<%# Bind("Discriminator") %>'
                        OnCheckedChanged="chkSelected_CheckedChanged" AutoPostBack="true" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Payload") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
</div>
</form>

代码背后:

public partial class _Default : System.Web.UI.Page
{
    SampleConnectionClassDataContext sampleContext = new SampleConnectionClassDataContext();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindCheckedGrid();
            BindNonCheckedGrid();
        }
    }

    private void BindCheckedGrid()
    {
        var checkedItems = from check in sampleContext.CheckboxSamples
                           where check.Discriminator == true
                           select check;

        grdSelected.DataSource = checkedItems;
        grdSelected.DataBind();
    }

    private void BindNonCheckedGrid()
    {
        var checkedItems = from check in sampleContext.CheckboxSamples
                           where check.Discriminator == false
                           select check;

        grdNotSelected.DataSource = checkedItems;
        grdNotSelected.DataBind();
    }


    protected void chkSelected_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox checkbox = (CheckBox)sender;
        GridViewRow row = (GridViewRow)checkbox.NamingContainer;
        Label lblID = (Label)row.FindControl("lblCheckedID");

        var existingEntry = from s in sampleContext.CheckboxSamples
                            where s.ID == int.Parse(lblID.Text)
                            select s;

        existingEntry.First().Discriminator = checkbox.Checked;
        sampleContext.SubmitChanges();

        BindCheckedGrid();
        BindNonCheckedGrid();

    }
}

希望这会有所帮助: - )