だから私はこれらの 2 つの Gridviews を持っています。「GridView1」という名前の最初のものは正常に機能しますが、「Gridview2」という名前の2番目のものは、ButtonFieldをクリックしてGridView ID列セル内からIDを取得すると、正しい行インデックスを取得しますが、間違ったGridViewから、機能 ButtonField のコードが GridViewCommandEventArgs _RowCommand コード ブロック内にある 'GridView2' ではなく 'GridView1' から取得します。e.CommandSource(.ID.toString()) でさえ、それが GridView2 からのものであると言っていますが、私が取得した列 ID は最初のものから取得されます. 2 つの GridView は同一ですが、2 番目の GridView にはより多くの ButtonFields があり、最初の GridView のカスタム ビューです。選択した ID のみが 2 番目の GridView(GridView2) に表示されます。これは私の最初の投稿のようなものです。あなたの助けに感謝します。Googleで検索してみましたが、私の問題は非常に具体的なようで、オンラインで見つけやすくするのは難しいです.
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;
}
}
それはおそらくとても単純なことであり、私はただ考えすぎています. あなたが助けることができればありがとう。