2015-11-06 46 views
1

今天我有幾個問題,我是ASP和C#中的新手,所以我需要一些幫助,問題在於下一個。 (我會嘗試最具體的可能)在gridview中處理按鈕事件

我已經創建了一個GridView,我從我的數據庫中填充,在我的GridView中,我添加了一列按鈕(詳細信息),這裏是問題。當我點擊一個按鈕時,我需要在瀏覽器中打開一個新窗口(我如何從C#執行此操作,或者僅在客戶端使用javascrip?),並且在需要顯示數據庫中的詳細信息後,首先需要從該按鈕的行中獲取ID,即GridView中的列ID,那麼如何獲取ID列的行值(單擊該按鈕)?

這是我的ASP代碼的GridView

<asp:GridView ID="GridView1" runat="server" 
AutoGenerateColumns="false" 
AllowPaging="true" CellPadding="3" 
OnPageIndexChanging="grdData_PageIndexChanging" PagerSettings-Position="Top" 
PagerStyle-ForeColor="Orange" 
PageSize="10"> 
<Columns> 
    <asp:BoundField DataField="id" HeaderText="ID" /> 
    <asp:BoundField DataField="request_type" HeaderText="RequestType"/> 
    <asp:BoundField DataField="priority" HeaderText="Priority" /> 
    <asp:BoundField DataField="modality" HeaderText="Modality" /> 
    <asp:BoundField DataField="name" HeaderText="Name" /> 
    <asp:BoundField DataField="start_date" HeaderText="Start Date"/> 
    <asp:BoundField DataField="end_date" HeaderText="End Date" /> 
    <asp:BoundField DataField="hour" HeaderText="Start Hour" /> 
    <asp:BoundField DataField="requester" HeaderText="Requester Name" /> 
    <asp:TemplateField> 
     <ItemTemplate> 
      <asp:Button ID="Details" runat="server" 
       CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" 
       Text="Details" CssClass="botonformulario"/> 
     </ItemTemplate> 
    </asp:TemplateField> 
</Columns> 

我試試這個在後面的代碼,但我認爲不能正常工作。

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
    { 
     int index = Convert.ToInt32(e.CommandArgument); 
     GridViewRow row = GridView1.Rows[index]; 
     //my code here.. 

    } 

我試過在這個頁面做同樣的事情。

http://www.dotnetbull.com/2013/05/how-to-handle-click-event-of-linkbutton.html

和這一個了,

https://msdn.microsoft.com/en-us/library/bb907626.aspx

我真的不知道我在做什麼錯了,當我按一下按鈕沒有任何反應。 我試過放置斷點,但我注意到斷點從不發生

非常感謝您的時間,任何評論都會有所幫助。

回答

0
<asp:GridView ID="GridView1" runat="server" 
    AutoGenerateColumns="false" 
    AllowPaging="true" CellPadding="3" 
    OnPageIndexChanging="grdData_PageIndexChanging" PagerSettings-Position="Top" 
    PagerStyle-ForeColor="Orange" 
    PageSize="10"> 
    <Columns> 
<asp:BoundField DataField="id" HeaderText="ID" /> 
<asp:BoundField DataField="request_type" HeaderText="RequestType"/> 
<asp:BoundField DataField="priority" HeaderText="Priority" /> 
<asp:BoundField DataField="modality" HeaderText="Modality" /> 
<asp:BoundField DataField="name" HeaderText="Name" /> 
<asp:BoundField DataField="start_date" HeaderText="Start Date"/> 
<asp:BoundField DataField="end_date" HeaderText="End Date" /> 
<asp:BoundField DataField="hour" HeaderText="Start Hour" /> 
<asp:BoundField DataField="requester" HeaderText="Requester Name" /> 
<asp:TemplateField> 
    <ItemTemplate> 
     <asp:Button ID="Details" runat="server" 
      CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" 
      Text="Details" OnClick="Details_Click" CommandName='<%# Eval("id")%>' CssClass="botonformulario"/> 
    </ItemTemplate> 
</asp:TemplateField> 

protected void Details_Click(object sender, EventArgs e) 
{ 
      Button btn=(Button)sender; 
      Response.Redirect("Details.aspx?id="+btn.CommandName); 
} 
0

你有GridView1_RowCommand處理程序,但它沒有被綁定在你發佈的html中。你可能需要綁定GridView.RowCommand該事件GridView

<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand" 
AutoGenerateColumns="false" 
AllowPaging="true" CellPadding="3" 
OnPageIndexChanging="grdData_PageIndexChanging" PagerSettings-Position="Top" 
PagerStyle-ForeColor="Orange" 
PageSize="10"> 

一旦你得到了觸發的事件,你需要確定源出於此,你會用CommandName

<asp:Button ID="Details" runat="server" CommandName="cmdDetails" 
      CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" 
      Text="Details" CssClass="botonformulario"/> 
+0

謝謝,我會試試這個:) –