同一页面上的多个Gridview,具有多个GridViewCommandEventArgs,buttonfield从错误的Gridview获取索引/ id

时间:2013-05-24 15:07:04

标签: c# asp.net events gridview

所以我有这两个Gridview。名为'GridView1'的第一个正常工作,但第二个名为'Gridview2',当我点击ButtonField从GridView ID列单元格中获取ID时,它获取正确的行索引,但是从错误的GridView,它从'GridView1'而不是'GridView2'获取它,其中功能ButtonField的代码在GridViewCommandEventArgs _RowCommand代码块中。我为我的生活无法弄清楚为什么它从第一个获得它,甚至e.CommandSource(.ID.toString())说它来自GridView2,但我得到的列ID是从第一个。两个GridView是相同的,除了第二个有更多的ButtonFields并且是第一个的自定义视图,只有选定的ID显示在第二个GridView(GridView2)上。这是我的第一篇文章,非常感谢您的帮助。尝试搜索谷歌,但我的问题看起来如此具体,有点难以让它在网上找到更广泛。

C#代码:一些全局变量如果有帮助:

public partial class userArea : System.Web.UI.Page {

static string getCustomView;
static string hash;
static string authNum;
static string addToMyCustomView;
static string getDistinctArray;
static string getFirstName;
static string getLastName;
static string getUsernameToAddToComp;
static string getNotes;
static string getUserSessionStatus;

ASPX代码:Gridview1

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
            AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" 
            DataSourceID="SqlDataSource1" ForeColor="#333333" PageSize="7" 
            Width="1344px" onrowcommand="GridView1_RowCommand">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
              <asp:BoundField DataField="lheID" HeaderText="ID" 
                    SortExpression="lheID" />
                <asp:BoundField DataField="lhewirelessGiant" HeaderText="Wireless Giant" 
                    SortExpression="lhewirelessGiant" />
                <asp:BoundField DataField="lheFName" HeaderText="First Name" 
                    SortExpression="lheFName" />
                <asp:BoundField DataField="lheInitials" HeaderText="Initials" 
                    SortExpression="lheInitials" />
                <asp:BoundField DataField="lheLName" HeaderText="Last Name" 
                    SortExpression="lheLName" />
                <asp:BoundField DataField="lhePrimaryEmail" HeaderText="Primary Email" 
                    SortExpression="lhePrimaryEmail" />
                <asp:BoundField DataField="lheDisplayName" HeaderText="Display Name" 
                    SortExpression="lheDisplayName" />
                <asp:BoundField DataField="lheNameID" HeaderText="Name ID" 
                    SortExpression="lheNameID" />
                    <asp:BoundField DataField="lheWhosLookingAtThis" HeaderText="Who's looking @ this?" 
                    SortExpression="lheNameID" />
                    <asp:ButtonField ButtonType="Image" ImageUrl="images/copy.png" CommandName="CopyToMe"
            HeaderText="Add to My List" Text="Add to My List" />

            </Columns>
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>

ASPX代码:GridView2

<asp:GridView ID="GridView2" runat="server" AllowPaging="True" 
            AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" 
            DataSourceID="SqlDataSource2" ForeColor="#333333" PageSize="7" 
            Width="1344px" onrowcommand="GridView2_RowCommand">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
             <asp:BoundField DataField="lheID" HeaderText="ID" 
                    SortExpression="lheID" />
                <asp:BoundField DataField="lhewirelessGiant" HeaderText="Wireless Giant" 
                    SortExpression="lhewirelessGiant" />
                <asp:BoundField DataField="lheFName" HeaderText="First Name" 
                    SortExpression="lheFName" />
                <asp:BoundField DataField="lheInitials" HeaderText="Initials" 
                    SortExpression="lheInitials" />
                <asp:BoundField DataField="lheLName" HeaderText="Last Name" 
                    SortExpression="lheLName" />
                <asp:BoundField DataField="lhePrimaryEmail" HeaderText="Primary Email" 
                    SortExpression="lhePrimaryEmail" />
                <asp:BoundField DataField="lheDisplayName" HeaderText="Display Name" 
                    SortExpression="lheDisplayName" />
                <asp:BoundField DataField="lheNameID" HeaderText="Name ID" 
                    SortExpression="lheNameID" />


            <asp:ButtonField ButtonType="Image" ImageUrl="images/Apply.png" CommandName="Complete"
            HeaderText="Complete" Text="Add to My List" />
            <asp:ButtonField ButtonType="Image" ImageUrl="images/Display.png" CommandName="AddComputer"
            HeaderText="Comp.(+)" Text="Add to My List" />

            <asp:ButtonField ButtonType="Image" ImageUrl="images/Text.png" CommandName="AddNotes"
            HeaderText="View/Add Notes" Text="Add to My List" />
             <asp:ButtonField ButtonType="Image" ImageUrl="images/Info.png" CommandName="Info"
            HeaderText="More Info" Text="Add to My List" />

            </Columns>
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>

C#代码:GridView1_RowCommand

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    string key = e.CommandName;

    if (key == "CopyToMe")
    {

        GridView1.DataBind();
        GridView2.DataBind();


        int index = int.Parse(e.CommandArgument.ToString());
        GridView grid = (GridView)e.CommandSource;
        GridViewRow row = grid.Rows[index];
        TableCell tblCell = row.Cells[0];
        authNum = tblCell.Text;


        //update the who is
        try
        {

            MySqlConnection con = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["legalHoldConnString"].ConnectionString);
            con.Open();
            MySqlCommand cmd = new MySqlCommand("UPDATE lhentries SET lheWhosLookingAtThis = '" + getFirstName + " " + getLastName + "' WHERE lheID = '" + authNum + "'  ;");
            cmd.Connection = con;
            MySqlDataReader reader1 = cmd.ExecuteReader();
            reader1.Close();



        }
        catch (Exception ex)
        {


        }

        addToMyCustomView = null;

        //get the custum view from my row

        try
        {
            MySqlConnection con = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["legalHoldConnString"].ConnectionString);
            con.Open();
            MySqlCommand cmd = new MySqlCommand("SELECT lhuCustomeView FROM lhusers WHERE lhUUsername = '" + getUsername.Text + "' AND lhupassword = '" + hash + "';");
            cmd.Connection = con;
            addToMyCustomView = ((string)cmd.ExecuteScalar());
            con.Close(); con.Dispose();

            // Response.Write(getCustomView);
        }
        catch (Exception)
        {


        }
       // Response.Write(addToMyCustomView);
        //perform adding string operation

        if (addToMyCustomView == null)
        {
            addToMyCustomView = authNum;
        }
        else 
        {
            addToMyCustomView = addToMyCustomView + "," + authNum;
        }


        //update the users custome view
        try
        {

            MySqlConnection con = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["legalHoldConnString"].ConnectionString);
            con.Open();
            MySqlCommand cmd = new MySqlCommand("UPDATE lhusers SET lhuCustomeView = '" + addToMyCustomView + "' WHERE lhupassword='" + hash.ToString() + "' AND lhuUsername = '" + Session["userSessionU"].ToString() + "'  ;");
            cmd.Connection = con;
            MySqlDataReader reader1 = cmd.ExecuteReader();
            reader1.Close();



        }
        catch (Exception ex)
        {


        }

        //Response.Redirect("userArea.aspx");
        GridView1.DataBind();
        GridView2.DataBind();


        //get rid of duplicates in the array and then update

        //make char array to be used as split delimitter for the string
        char[] splitOn = { ',' };
        //split the string into a array
        string[] idElements = addToMyCustomView.Split(splitOn);
        //only takes var but, makes the array unique, with no duplicates making it distinct
        var temp = idElements.Distinct().ToArray();
        //convert the array back into a string delimitted by a comma
        string getDistinctArray = String.Join(",", temp);

        //FOR TESTING WHAT GETS PULLED FROM THE USERS CUSTOM VIEW
        //Response.Write(getDistinctArray.ToString() + "this is the one");
        Response.Write(authNum);
        //update with distinct array
        try
        {

            MySqlConnection con = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["legalHoldConnString"].ConnectionString);
            con.Open();
            MySqlCommand cmd = new MySqlCommand("UPDATE lhusers SET lhuCustomeView = '" + getDistinctArray + "' WHERE lhupassword='" + hash.ToString() + "' AND lhuUsername = '" + Session["userSessionU"].ToString() + "'  ;");
            cmd.Connection = con;
            MySqlDataReader reader1 = cmd.ExecuteReader();
            reader1.Close();



        }
        catch (Exception ex)
        {


        }

        GridView1.DataBind();
        GridView2.DataBind();

        Response.Write(grid.ID.ToString() + " - " + authNum + " - " + index);

    }

    GridView1.DataBind();
    GridView2.DataBind();

}

C#代码:GridView2_RowCommand

 protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
    string key = e.CommandName;
    //Add computer
    if (key == "AddComputer")
    {
        //Visuals
        temp.Visible = true;
        temp.Style.Add("width", "100%");
        temp.Style.Add("height", "100%");
        temp.Style.Add("top", "0px");
        temp.Style.Add("left", "0px");

        topPanel.Style.Add("width", "300px");
        topPanel.Style.Add("height", "220px");
        //topPanel.Style.Add("margin", "0px auto");
        //topPanel.Style.Add("margin", "0px auto");
        Button2.Visible = true;
        addAComputer.Visible = true;
        computerTable.Visible = true;
        temp.Visible = true;
        topPanel.Visible = true;

        //Functionality

        //////UNRESOLVED: THERE ARE TWO GRIDVIEWS. THE FIRST ONE NAME GRIDVIEW1 WORKS WITHOUT A HITCH, THE SECOND GRIDVIEW NAMES GRIDVIEW2, WHEN THE BUTTONFIELD IS CLICKED THE ROW FOR THE PHYSICAL GRIDVIEW ITSELF, THE INDEX IS OBTAINED CORRECTLY, FOR THE COMMANDSOURCE, WHEN CONVERTING THE ID OF THE COMMANDSOURCE SO AS TO SEE WHICH OF THE GRIDVIEWS THE COMMANDSOURCE IS COMING FROM, THE COMMANDSOURCE SAYS GRIDVIEW2 WHICH IS CORRECT. WHEN THE INDEX OF THE COMMAND ARGUMENT IS USED TO GET THE GRIDVIEWROW, IT DOES GET THE CORRECT ROW, BUT IT'S STILL POINTING TO THE WRONG GRIDVIEW WHICH IS GRIDVIEW1 WHEN IT SHOULD BE GRIDVIEW2

        int index = int.Parse(e.CommandArgument.ToString()); // gets the right index(row from the gridview itself)
        GridView grid2 = (GridView)e.CommandSource; // saying it gets the right gridview        
        GridViewRow row = grid2.Rows[index]; //correct row, wrong gridview


        TableCell tblCell = row.Cells[0];
        authNum = tblCell.Text;

        //TEST
       //int authNums = Convert.ToInt32(GridView1.DataKeys[Convert.ToInt32(e.)].Value);

        TableCell place7 = row.Cells[7];
        getUsernameToAddToComp = place7.Text;

        //Response.Write(getUsernameToAddToComp);
        Response.Write(grid2.ID.ToString() + " - " + authNum + " - " + index);
    }

    if (key == "AddNotes")
    {
        //Visuals

        temp.Visible = true;
        temp.Style.Add("width", "100%");
        temp.Style.Add("height", "100%");
        temp.Style.Add("top", "0px");
        temp.Style.Add("left", "0px");

        topPanel.Style.Add("width", "300px");
        topPanel.Style.Add("height", "220px");
        //topPanel.Style.Add("margin", "0px auto");
        //topPanel.Style.Add("margin", "0px auto");
        addNotesDiv.Visible = true;
        notesTable.Visible = true;
        notesTable.Style.Add("z-index", "9");
        //Functionality

        int index = int.Parse(e.CommandArgument.ToString());
        GridView grid = (GridView)e.CommandSource;
        GridViewRow row = grid.Rows[index];
        TableCell tblCell = row.Cells[0];
        authNum = tblCell.Text;

        //get notes from entries table using authnum if any
        try
        {
            MySqlConnection con = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["legalHoldConnString"].ConnectionString);
            con.Open();
            MySqlCommand cmd = new MySqlCommand("SELECT lheNotes FROM lhentries WHERE lheID = '" + authNum + "' ;");
            cmd.Connection = con;
            getNotes = ((string)cmd.ExecuteScalar());
            con.Close(); con.Dispose();

            // Response.Write(getCustomView);
        }
        catch (Exception)
        {


        }
        //insert notes from db into text box

        noteTextBox.Text = getNotes;
        getNotes = "";

        //TableCell place7 = row.Cells[7];
        //getNotes = place7.Text;

        //Response.Write(getUsernameToAddToComp);

    }

    if (key == "Info")
    {
        int index = int.Parse(e.CommandArgument.ToString());
        GridView grid = (GridView)e.CommandSource;
        GridViewRow row = grid.Rows[index];
        TableCell tblCell = row.Cells[0];
        authNum = tblCell.Text;

        Session["getIDForMoreInfo"] = authNum;

        Response.Redirect("allUserInfo.aspx");
    }

    if (key == "Complete")
    {
        //Response.Write("");
        GridView2.DataBind();

        int index = int.Parse(e.CommandArgument.ToString());
        GridView grid = (GridView)e.CommandSource;
        GridViewRow row = grid.Rows[index];
        TableCell tblCell = row.Cells[0];
        authNum = tblCell.Text;

        //update the the entry to the status of completed
        try
        {

            MySqlConnection con = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["legalHoldConnString"].ConnectionString);
            con.Open();
            MySqlCommand cmd = new MySqlCommand("UPDATE lhentries SET lheCompleted = 'True' WHERE lheID = '" + authNum + "'; ");
            cmd.Connection = con;
            MySqlDataReader reader1 = cmd.ExecuteReader();
            reader1.Close();

            //this is a test to see if the button is picking up the correct row number
           // Response.Write(authNum);
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());

        }

        //GridView2.DataSource = "SqlDataSource2";
        GridView2.DataBind();
       // Response.Redirect("userArea.aspx");
        authNum = null;

    }


}

这可能是一件非常简单的事情,我只是在思考。谢谢,如果你能提供帮助。

1 个答案:

答案 0 :(得分:0)

事实证明,我偶然发现的问题是因为我的页面加载了一些东西。

protected void Page_Load(object sender, EventArgs e)
{
    //Session
    if (Session["userSessionU"] == null)
    {
        Response.Redirect("LHLogin.aspx");
    }
    else
    {
        getUsername.Text = Session["userSessionU"].ToString();
    }
   getUserSessionStatus =  Session["userAccountType"].ToString();
    //if super admin, show super admin controls
   if (getUserSessionStatus == "Super-Admin")
   {
       adminOptions.Visible = true;
   }
   else
   {
       adminOptions.Visible = false;
   }

    if (!IsPostBack)
    {
        GridView2.DataSourceID = "SqlDataSource1";
        GridView1.DataBind();
        GridView2.DataSourceID = "SqlDataSource2";
        GridView2.DataBind();


    }

    //display for divs
   // topPanel.Visible = false;
    temp.Attributes.CssStyle.Add("opacity", "0.6");
    //computerTable.Visible = false;

    //refresh gridviews

    SqlDataSource2.DataBind();
    GridView2.DataBind();

    //hash password session string
    hash = FormsAuthentication.HashPasswordForStoringInConfigFile(Session["userSessionPW"].ToString() + "", "SHA1");

    //get the users last name

    try
    {
        MySqlConnection con = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["legalHoldConnString"].ConnectionString);
        con.Open();
        MySqlCommand cmd = new MySqlCommand("SELECT lhulname FROM lhusers WHERE lhUUsername = '" + getUsername.Text + "' AND lhupassword = '" + hash + "';");
        cmd.Connection = con;
        getLastName = ((string)cmd.ExecuteScalar());
        con.Close(); con.Dispose();

        // Response.Write(getCustomView);
    }
    catch (Exception)
    {


    }

    //get the users first name

    try
    {
        MySqlConnection con = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["legalHoldConnString"].ConnectionString);
        con.Open();
        MySqlCommand cmd = new MySqlCommand("SELECT lhufname FROM lhusers WHERE lhUUsername = '" + getUsername.Text + "' AND lhupassword = '" + hash + "';");
        cmd.Connection = con;
        getFirstName = ((string)cmd.ExecuteScalar());
        con.Close(); con.Dispose();

        // Response.Write(getCustomView);
    }
    catch (Exception)
    {


    }

    try
    {
        MySqlConnection con = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["legalHoldConnString"].ConnectionString);
        con.Open();
        MySqlCommand cmd = new MySqlCommand("SELECT lhuCustomeView FROM lhusers WHERE lhUUsername = '" + getUsername.Text + "' AND lhupassword = '"+ hash +"';");
        cmd.Connection = con;
        getCustomView = ((string)cmd.ExecuteScalar());
        con.Close(); con.Dispose();

       // Response.Write(getCustomView);
    }
    catch (Exception)
    {


    }




    //clear the customview string


    //adv search options for entries
    //------   ----------------------


    //////////////////////////////////////////////////////////////
    searchBtn.Focus();
    SqlDataSource1.SelectCommand = "SELECT lheID, lhewirelessGiant, lheFName, lheInitials, lheLName, lhePrimaryEmail, lheDisplayName, lheNameID, lheWhosLookingAtthis FROM lhentries  WHERE (lheNameID LIKE '%" + searchEntryTable.Text.ToString() + "%' ) ORDER BY lheID DESC";
    if (firstNameRB.Checked)
    {
        SqlDataSource1.SelectCommand = "SELECT lheID, lhewirelessGiant, lheFName, lheInitials, lheLName, lhePrimaryEmail, lheDisplayName, lheNameID, lheWhosLookingAtthis FROM lhentries  WHERE (lheFName LIKE '%" + searchEntryTable.Text.ToString() + "%' ) ORDER BY lheID DESC";
    }
    else if (lastNameRB.Checked)
    {
        SqlDataSource1.SelectCommand = "SELECT lheID, lhewirelessGiant, lheFName, lheInitials, lheLName, lhePrimaryEmail, lheDisplayName, lheNameID, lheWhosLookingAtthis FROM lhentries  WHERE (lheLName LIKE '%" + searchEntryTable.Text.ToString() + "%' ) ORDER BY lheID DESC";
    }
    else if (emailRB.Checked)
    {
        SqlDataSource1.SelectCommand = "SELECT lheID, lhewirelessGiant, lheFName, lheInitials, lheLName, lhePrimaryEmail, lheDisplayName, lheNameID, lheWhosLookingAtthis FROM lhentries  WHERE (lhePrimaryEmail LIKE '%" + searchEntryTable.Text.ToString() + "%' ) ORDER BY lheID DESC";
    }
    else if (displayNameRB.Checked)
    {
        SqlDataSource1.SelectCommand = "SELECT lheID, lhewirelessGiant, lheFName, lheInitials, lheLName, lhePrimaryEmail, lheDisplayName, lheNameID, lheWhosLookingAtthis FROM lhentries  WHERE (lheDisplayName LIKE '%" + searchEntryTable.Text.ToString() + "%' ) ORDER BY lheID DESC";
    }
    else if (nameID.Checked)
    {
        SqlDataSource1.SelectCommand = "SELECT lheID, lhewirelessGiant, lheFName, lheInitials, lheLName, lhePrimaryEmail, lheDisplayName, lheNameID, lheWhosLookingAtthis FROM lhentries  WHERE (lheNameID LIKE '%" + searchEntryTable.Text.ToString() + "%' ) ORDER BY lheID DESC";
    }
    else if (whosLookingAtThis.Checked)
    {
        SqlDataSource1.SelectCommand = "SELECT lheID, lhewirelessGiant, lheFName, lheInitials, lheLName, lhePrimaryEmail, lheDisplayName, lheNameID, lheWhosLookingAtthis FROM lhentries  WHERE (lheWhosLookingAtthis LIKE '%" + searchEntryTable.Text.ToString() + "%' ) ORDER BY lheID DESC";
    }
    else if (wirelessGiantRB.Checked)
    {
        SqlDataSource1.SelectCommand = "SELECT lheID, lhewirelessGiant, lheFName, lheInitials, lheLName, lhePrimaryEmail, lheDisplayName, lheNameID, lheWhosLookingAtthis FROM lhentries  WHERE (lhewirelessGiant LIKE '%" + searchEntryTable.Text.ToString() + "%' ) ORDER BY lheID DESC";
    }
    else 
    {
        nameID.Checked = true;
    }

    //////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //for my table entries
    if (Session["getSearchForMyTable"] != null)
   {

       SqlDataSource2.SelectCommand = "SELECT lheID, lhewirelessGiant, lheFName, lheInitials, lheLName, lhePrimaryEmail, lheDisplayName, lheNameID, lheWhosLookingAtthis FROM lhentries  WHERE  lheID IN (" + getCustomView + ") AND (lheNameID LIKE '%" + searchMyTable.Text.ToString() + "%' ) AND lheCompleted = 'False' ORDER BY lheID DESC;";

    if (firstNameRB.Checked)
    {
        SqlDataSource2.SelectCommand = "SELECT lheID, lhewirelessGiant, lheFName, lheInitials, lheLName, lhePrimaryEmail, lheDisplayName, lheNameID, lheWhosLookingAtthis FROM lhentries  WHERE  lheID IN (" + getCustomView + ") AND (lheFName LIKE '%" + searchMyTable.Text.ToString() + "%' ) AND lheCompleted = 'False' ORDER BY lheID DESC;";

    }
    else if (lastNameRB.Checked)
    {
        SqlDataSource2.SelectCommand = "SELECT lheID, lhewirelessGiant, lheFName, lheInitials, lheLName, lhePrimaryEmail, lheDisplayName, lheNameID, lheWhosLookingAtthis FROM lhentries  WHERE  lheID IN (" + getCustomView + ") AND (lheLName LIKE '%" + searchMyTable.Text.ToString() + "%' )  AND lheCompleted = 'False' ORDER BY lheID DESC;";

    }
    else if (emailRB.Checked)
    {
        SqlDataSource2.SelectCommand = "SELECT lheID, lhewirelessGiant, lheFName, lheInitials, lheLName, lhePrimaryEmail, lheDisplayName, lheNameID, lheWhosLookingAtthis FROM lhentries  WHERE  lheID IN (" + getCustomView + ") AND (lhePrimaryEmail LIKE '%" + searchMyTable.Text.ToString() + "%' )  AND lheCompleted = 'False' ORDER BY lheID DESC;";

    }
    else if (displayNameRB.Checked)
    {
        SqlDataSource2.SelectCommand = "SELECT lheID, lhewirelessGiant, lheFName, lheInitials, lheLName, lhePrimaryEmail, lheDisplayName, lheNameID, lheWhosLookingAtthis FROM lhentries  WHERE  lheID IN (" + getCustomView + ") AND (lheDisplayName LIKE '%" + searchMyTable.Text.ToString() + "%' )  AND lheCompleted = 'False' ORDER BY lheID DESC;";

    }
    else if (nameID.Checked)
    {
        SqlDataSource2.SelectCommand = "SELECT lheID, lhewirelessGiant, lheFName, lheInitials, lheLName, lhePrimaryEmail, lheDisplayName, lheNameID, lheWhosLookingAtthis FROM lhentries  WHERE  lheID IN (" + getCustomView + ") AND (lheNameID LIKE '%" + searchMyTable.Text.ToString() + "%' )  AND lheCompleted = 'False' ORDER BY lheID DESC;";


        // Response.Write("this test works if u can se ethis");
    }
    else if (whosLookingAtThis.Checked)
    {
        SqlDataSource2.SelectCommand = "SELECT lheID, lhewirelessGiant, lheFName, lheInitials, lheLName, lhePrimaryEmail, lheDisplayName, lheNameID, lheWhosLookingAtthis FROM lhentries  WHERE  lheID IN (" + getCustomView + ") AND (lheWhosLookingAtthis LIKE '%" + searchMyTable.Text.ToString() + "%' ) AND lheCompleted = 'False' ORDER BY lheID DESC;";

    }
    else if (wirelessGiantRB.Checked)
    {
        SqlDataSource2.SelectCommand = "SELECT lheID, lhewirelessGiant, lheFName, lheInitials, lheLName, lhePrimaryEmail, lheDisplayName, lheNameID, lheWhosLookingAtthis FROM lhentries  WHERE  lheID IN (" + getCustomView + ") AND (lhewirelessGiant LIKE '%" + searchMyTable.Text.ToString() + "%' )  AND lheCompleted = 'False' ORDER BY lheID DESC;";


    }
   }


    if ((getCustomView == null) )
    {
        //Perform the custom view with the select command
        SqlDataSource2.SelectCommand = "SELECT lheID, lhewirelessGiant, lheFName, lheInitials, lheLName, lhePrimaryEmail, lheDisplayName, lheNameID, lheWhosLookingAtthis FROM lhentries WHERE lheID  = 0  AND lheCompleted = 'False' ORDER BY lheID DESC; ";
    }
    else
    {

        try
        {

            //Perform the custom view with the select command
            SqlDataSource2.SelectCommand = "SELECT lheID, lhewirelessGiant, lheFName, lheInitials, lheLName, lhePrimaryEmail, lheDisplayName, lheNameID, lheWhosLookingAtthis FROM lhentries WHERE lheID IN (" + getCustomView + ")  AND lheCompleted = 'False' ORDER BY lheID DESC; ";
        }
        catch (Exception ex) { }
    }


    getCustomView = null;
}

但主要问题出在这个代码块中:

if ((getCustomView == null) )
    {
        //Perform the custom view with the select command
        SqlDataSource2.SelectCommand = "SELECT lheID, lhewirelessGiant, lheFName, lheInitials, lheLName, lhePrimaryEmail, lheDisplayName, lheNameID, lheWhosLookingAtthis FROM lhentries WHERE lheID  = 0  AND lheCompleted = 'False' ORDER BY lheID DESC; ";
    }
    else
    {

        try
        {

            //Perform the custom view with the select command
            SqlDataSource2.SelectCommand = "SELECT lheID, lhewirelessGiant, lheFName, lheInitials, lheLName, lhePrimaryEmail, lheDisplayName, lheNameID, lheWhosLookingAtthis FROM lhentries WHERE lheID IN (" + getCustomView + ")  AND lheCompleted = 'False' ORDER BY lheID DESC; ";
        }
        catch (Exception ex) { }
    }

所以现在我必须找出一个更好的select语句,这样当自定义视图变为null时,它不会默认为第一个数据源,从外观上看它从不同的数据源获取相同的id。可能必须使用一些内联脚本,我仍然在接受意见,但这应该可以解决这个问题。