2

我在 asp.net 中使用 GridView。我想选择一个数据行。我在属性面板中查找了 MultiSelect 和 SelectionMode,但找不到。

那么如何在 GridView 中启用选择行呢?

谢谢。

在此处输入图像描述 代码背后

public partial class SearchCourse : System.Web.UI.Page
{
Connection dbCon;
DataTable tbl;

protected void Page_Load(object sender, EventArgs e)
{
    dbCon = new Connection();


}
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
    if (RadioButton1.Checked) {
        txtSubName.Enabled = true;
        comboSemester.Enabled = false;
        comboYear.Enabled = false;
        comboProgram.Enabled =false;
        txtSubName.Text = "";
    }
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
    if (RadioButton2.Checked) {

        comboProgram.Enabled = true;

        if (comboProgram.SelectedItem.ToString() == "Foundation Course")
        {
            comboSemester.Enabled = false;
            comboYear.Enabled = false;
        }
        else {
            comboSemester.Enabled = true;
            comboYear.Enabled = true;
        }
        txtSubName.Text = "";
        txtSubName.Enabled = false;
    }
}

protected void imgBtnSearch_Click(object sender, ImageClickEventArgs e)
{
    if (RadioButton1.Checked) {
        String name = txtSubName.Text;
        tbl = dbCon.getResultsBySubjectName(name);
        GridView1.DataSource = tbl;
        GridView1.DataBind();
    }
    else if (RadioButton2.Checked)
    {
        String program = comboProgram.SelectedItem.ToString();
        String year = comboYear.SelectedItem.ToString();
        String sem= comboSemester.SelectedItem.ToString();
        tbl = dbCon.getResultsByProgram(program,year,sem);
        GridView1.DataSource = tbl;
        GridView1.DataBind();
    }
    else if (RadioButton3.Checked)
        {
            String name = txtSubName.Text;
            tbl = dbCon.getResultsBySubjectNo(name);
            GridView1.DataSource = tbl;
            GridView1.DataBind();
        }

}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    String program = comboProgram.SelectedItem.ToString();
    String year, sem;
    if (program == "Foundation Course")
    {
        comboYear.Enabled = false;
        comboSemester.Enabled = false;
        year = null;
        sem = null;
    }
    else {
        comboYear.Enabled = true;
        comboSemester.Enabled = true;
        year = comboYear.SelectedItem.ToString();
        sem = comboSemester.SelectedItem.ToString();
    }

    tbl = dbCon.getResultsByProgram(program, year, sem);
    GridView1.DataSource = tbl;
    GridView1.DataBind();
}

protected void comboYear_SelectedIndexChanged(object sender, EventArgs e)
{
    String program = comboProgram.SelectedItem.ToString();
    String year = comboYear.SelectedItem.ToString();
    String sem = comboSemester.SelectedItem.ToString();
    tbl = dbCon.getResultsByProgram(program, year, sem);
    GridView1.DataSource = tbl;
    GridView1.DataBind();
}
protected void comboSemester_SelectedIndexChanged(object sender, EventArgs e)
{
    String program = comboProgram.SelectedItem.ToString();
    String year = comboYear.SelectedItem.ToString();
    String sem = comboSemester.SelectedItem.ToString();
    tbl = dbCon.getResultsByProgram(program, year, sem);
    GridView1.DataSource = tbl;
    GridView1.DataBind();

}



protected void RadioButton3_CheckedChanged(object sender, EventArgs e)
{
    if (RadioButton3.Checked)
    {
        txtSubName.Enabled = true;
        comboSemester.Enabled = false;
        comboYear.Enabled = false;
        comboProgram.Enabled = false;
        txtSubName.Text = "";
    }
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{

}

}

网格视图代码

<asp:GridView ID="GridView1" CssClass="grid" runat="server" AllowPaging="True" 
BorderColor="Black" BorderStyle="Solid" BorderWidth="2px" 
GridLines="Horizontal"  EnableViewState="False" 
PageSize="5"   onselectedindexchanged="GridView1_SelectedIndexChanged" >

<RowStyle CssClass="gridRow" Width="800px" />

<SelectedRowStyle BackColor="#FF0066" ForeColor="White" />

</asp:GridView> 
4

2 回答 2

0

我认为 MultiSelect 和 SelectionMode 属性仅适用于 VB.NET 网格,而不适用于 ASP.NET。请记住,ASP.NET 中的所有控件都是伪装的 HTML,因此它们可能会受到更多限制。您没有理由不能拥有多选表,但您必须自己做管道。因此,您需要通过处理 RowDataBound 事件来启用行选择,如

http://forums.asp.net/t/992062.aspx?How+to+select+row+in+gridview+on+click

或者使用 MS 提供的选项,如

http://msdn.microsoft.com/en-us/library/wbk82279(v=vs.100).aspx

然后你需要处理 SelectedIndexChanging 事件,找出用户点击了哪一行,然后自己处理行着色。

于 2014-02-24T09:48:11.163 回答
-1

在 gridview 中,您必须定义一个事件onselectedindexchangedonrowdatabound如下所示:

onselectedindexchanged="GridView1_SelectedIndexChanged"  onrowdatabound="GridView1_RowDataBound"  

要显示选定的行,您可以在网格视图中使用以下样式:

 <SelectedRowStyle BackColor="Red" />

在后面的代码中:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // Set the hand mouse cursor for the selected row.
        e.Row.Attributes.Add("OnMouseOver", "this.style.cursor = 'hand';");

        // The seelctButton exists for ensuring the selection functionality
        // and bind it with the appropriate event hanlder.
        LinkButton selectButton = new LinkButton()
        {
            CommandName = "Select",
            Text = e.Row.Cells[0].Text
        };
        selectButton.Font.Underline = false;
        selectButton.ForeColor = Color.Black;

        e.Row.Cells[0].Controls.Add(selectButton);
        //e.Row.Attributes["OnClick"] =
        //     Page.ClientScript.GetPostBackClientHyperlink(selectButton, "");
        e.Row.Attributes["onclick"] =         ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
    }
    }

注意:您可以在事件窗口中找到该事件。

于 2013-04-06T04:13:17.890 回答