所以我有这两个Gridviews。第一个名为“GridView1”的工作正常,但第二个名为“Gridview2”,当我单击 ButtonField 以从 GridView ID 列单元格内部获取 ID 时,它获取正确的行索引,但来自错误的 GridView,它从 'GridView1' 而不是 'GridView2' 获取它,其中 ButtonField 功能的代码位于 GridViewCommandEventArgs _RowCommand 代码块内。我一生都无法弄清楚为什么它是从第一个获取的,即使 e.CommandSource(.ID.toString()) 说它来自 GridView2,但是我得到的列 ID 是从第一个获取的. 这两个 GridView 是相同的,除了第二个有更多的 ButtonField 并且是第一个的自定义视图,只有选定的 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;
}
}
这可能是一件很简单的事情,我只是想多了。谢谢,如果你能帮忙。