簡體   English   中英

c#gridview行單擊

[英]c# gridview row click

當我點擊GridView中的一行時,我想轉到另一個頁面,其中包含我從數據庫中獲取的ID。

在我的RowCreated事件中,我有以下行:

e.Row.Attributes.Add(
     "onClick",
     ClientScript.GetPostBackClientHyperlink(
          this.grdSearchResults, "Select$" + e.Row.RowIndex));

為了防止錯誤消息我有這個代碼:

protected override void Render(HtmlTextWriter writer)
{
    // .NET will refuse to accept "unknown" postbacks for security reasons. 
    // Because of this we have to register all possible callbacks
    // This must be done in Render, hence the override
    for (int i = 0; i < grdSearchResults.Rows.Count; i++)
    {
        Page.ClientScript.RegisterForEventValidation(
                new System.Web.UI.PostBackOptions(
                    grdSearchResults, "Select$" + i.ToString()));
    }
    // Do the standard rendering stuff
    base.Render(writer);
}

如何為行添加一個唯一的ID(來自數據庫),當我單擊該行時,將打開另一個頁面(如單擊某個href),該頁面可以讀取該ID。

馬亭,

這是另一個帶有一些漂亮的行突出顯示和一個href樣式游標的例子:

protected void gvSearch_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
    e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ceedfc'");
    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
    e.Row.Attributes.Add("style", "cursor:pointer;");
    e.Row.Attributes.Add("onclick", "location='patron_detail.aspx?id=" + e.Row.Cells[0].Text + "'");
  }
}

上面的代碼適用於.NET 3.5。 但是,您不能將您的id列設置為Visible =“false”,因為您將獲得id鍵的空白查詢字符串值:

<asp:GridView ID="gvSearch" runat="server" OnRowDataBound="gvSearch_RowDataBound" AutoGenerateColumns="false">
  <Columns>
    <asp:BoundField DataField="id" Visible="false" />
    <asp:BoundField DataField="first_name" HeaderText="First" />
    <asp:BoundField DataField="last_name" HeaderText="Last" />
    <asp:BoundField DataField="email" HeaderText="Email" />
    <asp:BoundField DataField="state_name" HeaderText="State" />
  </Columns>
</asp:GridView>

因此,將第一列更改為:

<asp:BoundField DataField="id" ItemStyle-CssClass="hide" />

將此css添加到頁面頂部:

<head>
  <style type="text/css">
    .hide{
      display:none;
    }
  </style>
<head>

但要隱藏標題行的第一個單元格,請將其添加到代碼隱藏中的gvSearch_RowDataBound():

if (e.Row.RowType == DataControlRowType.Header)
{
  e.Row.Cells[0].CssClass = "hide";
}

顯然,您也可以在代碼隱藏中隱藏id列,但這會導致標記中的文本比css類更多:

e.Row.Cells[0].Attributes.Add("style", "display:none;");
e.Row.Attributes.Add("style", "cursor:pointer;");

我有解決方案。

這就是我所做的:

if(e.Row.RowType == DataControlRowType.DataRow)
{
    e.Row.Attributes["onClick"] = "location.href='view.aspx?id=" + DataBinder.Eval(e.Row.DataItem, "id") + "'";
}

我已將前面的代碼放在RowDataBound事件中。

protected void gvSearch_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string abc = ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();
        e.Row.Attributes["onClick"] = "location.href='Default.aspx?id=" + abc + "'";    
    }
}
protected void gvSearch_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
        string abc = ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString(); 
        e.Row.Attributes["onClick"] = "location.href='Default.aspx?id=" + abc + "'";     
    } 
} 
protected void gvSearch_RowDataBound(object sender, GridViewRowEventArgs e)  
{     
 if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridViewRow gvr = e.Row;
            string abc = ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();         
            gvr.Attributes.Add("OnClick", "javascript:location.href='Default.aspx?id=" + abc + "'");
        }         

   }  
}  

您可以使用網格視圖的RowCommand事件。 在您要設置單擊的按鈕/鏈接中,設置CommandName和CommandArgument,您可以在事件方法的EventArgs參數處加入。

您的ID是否可以與gridview中顯示的數據項相關聯?

如果是這樣,您可以使用e.Row.DataItem,並將其強制轉換為任何類型。

JohnB,你的代碼工作得非常好,我添加了一點點黑客以避免在mouseout后使用alternatingRowStyle破壞。

e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");

變成:

e.Row.Attributes.Add("onmouseout", "if(" + e.Row.RowIndex + "% 2 == 0) { this.style.backgroundColor=''; } else { this.style.backgroundColor = '#E8F7EA'; }");

如果有更好的方法,請告訴我,但它對我來說非常合適。

問候。

在網格視圖中單擊行重定向到其他頁面

    protected void gvSearch_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
             string abc = ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();
             e.Row.Attributes["onClick"] = "location.href='Default.aspx?id=" + abc + "'";
        }
    }

工作得非常好

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM