checkBox in gridView

本文关键字:gridView in checkBox | 更新日期: 2023-09-27 17:49:56

我下面的代码不能正常工作…代码是关于当用户检查与网格视图附加的复选框,这个信息应该触发到表,一旦数据被发送到数据库和两个视图将根据条件创建,如果复选框是检查或不..和视图的结果应该显示在一个标签框。(在ASP。. NET USING c# AND 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);
}

checkBox in gridView

我不确定您在第一部分问的是什么。至于底部的问题:

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

"。NamingContainer"将获得包含你的复选框的对象,在这种情况下,你的复选框在GridViewRow中。你仍然需要将对象转换为GridViewRow,这是"(GridViewRow)"的目的。您正在将其分配给本地变量行,以便您可以继续使用它。

好吧,我从来没有通过SQL处理过视图。我通过查询来处理视图。下面是我如何处理这个问题的例子。我认为它符合你的要求。这是我的完整代码。你应该能够复制粘贴看到它运行除了我使用linq到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();
    }
}

希望有帮助:-)