Asp.net GridView启用行选择

时间:2013-04-06 03:58:16

标签: asp.net gridview

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

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

感谢。

enter image description here 代码背后

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

}

}

GridView代码

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

2 个答案:

答案 0 :(得分: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事件,弄清楚用户点击了哪一行,并自己处理行着色。

答案 1 :(得分:-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);
    }
    }

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