508 掃描標記了我們在網格視圖中的按鈕具有相同 ID 的事實,我想知道如何解決這個問題。對于其中一些按鈕,我們可以完全洗掉 id,它們似乎不會影響功能,但其他按鈕實際上確實有后端代碼,可以根據其他因素啟用/禁用它們。這是前端代碼:
<asp:TemplateField HeaderText="Accept" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Button
ID="btnAccept"
runat="server"
Text="Accept Data"
CssClass="inherited"
CommandName="AcceptData"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:TemplateField>
以下是與此按鈕相關的一些后端代碼:
protected void gvDashboard_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int rowIndex = e.Row.RowIndex;
Button btnAccept = e.Row.FindControl(@"btnAccept") as Button;
Button btnRemove = e.Row.FindControl(@"btnRemove") as Button;
bool errors = (bool)gvDashboard.DataKeys[e.Row.RowIndex][@"Errors"];
string fileType = (string)gvDashboard.DataKeys[e.Row.RowIndex][@"FileType"];
string processingStatus = (string)gvDashboard.DataKeys[e.Row.RowIndex][@"ProcessingStatus"];
bool accepted = (bool)gvDashboard.DataKeys[e.Row.RowIndex][@"Accepted"];
int records = 0;
if (gvDashboard.DataKeys[e.Row.RowIndex][@"Records"] is System.DBNull)
{
int recordsColumnIndex = Utilities.GetColumnIndexByHeaderText(gvDashboard, @"Records");
e.Row.Cells[recordsColumnIndex].Text = @"0";
}
else
{
records = (int)gvDashboard.DataKeys[e.Row.RowIndex][@"Records"];
}
if (fileType == @"CN" || fileType == @"CP")
{
if ((DBNull.Value.Equals(gvDashboard.DataKeys[e.Row.RowIndex][@"ZipId"]))
|| ((int)gvDashboard.DataKeys[e.Row.RowIndex][@"ZipId"] == 0))
{
// CN or CP did not come from a zip file
if ((accepted))
{
btnAccept.Enabled = false;
btnRemove.Enabled = false;
}
}
else
{
btnAccept.Enabled = false;
btnRemove.Enabled = false;
}
}
if (accepted || processingStatus == @"I" || processingStatus == @"N")
{
btnAccept.Enabled = false;
}
}
}
catch (Exception ex)
{
DAL.ErrorLog(@"FilteredDashboard.gvDashboard_RowDataBound: Row: " e.Row.RowIndex.ToString() " " ex.Message);
}
}
理想情況下,我希望能夠將自動生成的數字添加到前端 ID 上,然后使用相同的初始字串(如“btnAccept”或“btnRemove”)來說明 ID,而不管它們的附加數字后綴。這有可能嗎?
uj5u.com熱心網友回復:
ASP.NET Webforms 自動為每個控制元件生成按鈕 ID,除非您將其設定為
ClientIDMode="Static"
如果您想確保 ID 是唯一的,請設定
ClientIDMode="AutoID" //this is the default setting for all webform controls, and autogenerates an ID
所以問題可能來自其他地方,您可以在瀏覽器的檢查器中自行檢查。
uj5u.com熱心網友回復:
正如 Zee 在回答中所指出的那樣-您可以使用 find 控制元件為每一行使用一個名稱-但是,每個行控制元件-包括按鈕都具有自己的 ID。
這個問題不應該存在 - 作為一般規則,從來都不是問題。GV 的每一行都會為每一行控制元件自動生成新的 id。但是,在生成新的“id”時,您可以在行資料系結事件上使用帶有控制元件名稱的查找控制元件而不會出現問題。
此外,您真的不需要為 hte 按鈕命令 args 保存/放入行索引。事實上,您還可以獲得資料庫主鍵 - 甚至不顯示或在每一行中顯示 PK。
假設您設定了資料鍵(例如 PK“id”列),那么您可以使用簡單的按鈕行單擊事件來處理 gv 行上的按鈕。
您不能雙擊按鈕來添加事件(因為它在 GV 中),但您可以像這樣使用 intel-sense:
只需輸入 onclick= ,當你點擊“=”時,你會得到這樣的英特爾感覺:

因此,您可以選擇創建新事件。
現在,在這個平面簡按鈕事件中,您可以
Get any value or control of that row.
Get the row index - no need to put in markup
Get the row primary key database row ID - again no need to have in markup.
因此,按鈕后面的代碼可能如下所示:
protected void Button1_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
GridViewRow gRow = btn.NamingContainer as GridViewRow;
Debug.Print("Row index click = " gRow.RowIndex);
Debug.Print("Data base PK ID = " GridView1.DataKeys[gRow.RowIndex]["ID"]);
// And you are free to using gRow.FindControl, or even the cells collection
// for non templated values.
// eg: gRow.Cells[0].Text
輸出:
Row index click = 3
Data base PK ID = 11
Note how I used datakeys setting. This is REALLY nice for security, since I don't have to expose client side the database row PK id used. And note how I also picked up the current GV row index - again all without having to put or mess with that stuff in the actual gv row. So, you don't even need to use command Argument either. And you don't need to use commandName either. You ONLY need to set CommandName if you want the Gv selected index event to fire. (so, you might need it), but as a general rule, just drop in a plane jane button - wire up the event, and get the current row with "naming container" as I did. And then you get the GV row, and with that you have everything you need - including rowindex, and even use of datakeys as I pointed out.
In fact, this means for the most part I don't bother with the built in GV events - they are not worth your time in most cases.
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/442878.html
