0

我有一个网格视图来显示表格中的请求信息。我添加了一个按钮模板字段,点击后应将用户带到适当的请求 url

如何捕获选定的 requestid 并将其作为变量传递给我的重定向 url

我的网格视图

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    CellPadding="4" DataKeyNames="roc_id" DataSourceID="analystdefaultview" 
    ForeColor="#333333" GridLines="None" AllowPaging="True"  
    AllowSorting="True" Visible="False" 
        onselectedindexchanged="GridView1_SelectedIndexChanged"> 
    <AlternatingRowStyle BackColor="White" />
    <Columns>

               <asp:CommandField ShowDeleteButton = "true"  />
        <asp:TemplateField HeaderText="RequestId" SortExpression="roc_id">
            <EditItemTemplate>
                <asp:HyperLink ID="HyperLink1" runat="server" Text='<%# Bind("roc_id") %>'></asp:HyperLink>
            </EditItemTemplate>
            <ItemTemplate>
               <asp:Button ID = "button2" runat = "server" Text='<%# Bind("roc_id") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="fundtype" HeaderText="fundtype" 
            SortExpression="fundtype" />
        <asp:BoundField DataField="productionmth" HeaderText="productionmth" 
            SortExpression="productionmth" />
        <asp:BoundField DataField="targetdt" HeaderText="targetdt" 
            SortExpression="targetdt" ReadOnly="True" />
        <asp:BoundField DataField="actualdt" HeaderText="actualdt" 
            SortExpression="actualdt" ReadOnly="True" />
        <asp:BoundField DataField="freqcd" HeaderText="freqcd" 
            SortExpression="freqcd" />
        <asp:BoundField DataField="entereddt" HeaderText="entereddt" 
            SortExpression="entereddt" ReadOnly="True" />
        <asp:BoundField DataField="groupcnt" HeaderText="groupcnt" 
            SortExpression="groupcnt" />
        <asp:BoundField DataField="rptrsrcetxt" HeaderText="rptrsrcetxt" 
            SortExpression="rptrsrcetxt" />
        <asp:BoundField DataField="dateavailable" HeaderText="dateavailable" 
            SortExpression="dateavailable" ReadOnly="True" />
        <asp:BoundField DataField="groupname" HeaderText="groupname" 
            SortExpression="groupname" />
        <asp:BoundField DataField="groupnbr" HeaderText="groupnbr" 
            SortExpression="groupnbr" />
        <asp:BoundField DataField="datarecpdet" HeaderText="datarecpdet" 
            SortExpression="datarecpdet" />
    </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>






<asp:SqlDataSource ID="analystdefaultview" runat="server" 
    ConnectionString="<%$ ConnectionStrings:Underwriting %>" 
    SelectCommand="SELECT
       [roc_id]
       ,[fundtype]
       ,Convert(varchar(20),[prodmth],111) as productionmth
       ,Convert(varchar(20),[targetdt],111) as targetdt
       ,Convert(varchar(20),[actualdt],111) as actualdt
       ,[freqcd]
       ,Convert(varchar(20),[entereddt],111) as entereddt
       ,[groupcnt]
       ,[rptrsrcetxt]     
       ,Convert(varchar(20),[date_available],111) as dateavailable
       ,[groupname]
       ,[groupnbr]
       ,[datarecpdet]


        from renoffcyc where opa_id = @opaid and YEAR(prodmth) = YEAR(getdate()) AND MONTH(prodmth) = MONTH(getdate())"

     DeleteCommand = "DElete from renoffcyc where  roc_id = @roc_id" >


    <SelectParameters>
        <asp:ControlParameter ControlID="chooseanalyst" DefaultValue="1" Name="opaid" 
            PropertyName="SelectedValue" />
    </SelectParameters>

</asp:SqlDataSource>

</div>

.cs 文件- roc_id 是我需要传递给 url 的变量

 protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Redirect("http://xxx.yahoo.com/testing/adhoc/Edit.asp?ROCID=roc_id");

    }
4

2 回答 2

2

试试这个

protected void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e)
{    
     GridViewRow row = GridView1.SelectedRow;   
     string roc_id = GridView1.DataKeys[row.RowIndex].ToString();

     string url = String.Format("http://xxx.yahoo.com/testing/adhoc/Edit.asp?ROCID={0}", roc_id);
     Response.Redirect(url);   
}
于 2012-11-09T19:24:08.587 回答
0

您需要RowCommand事件以及用于在 RowCommmand 事件中传递值的CommandArgument

html

<ItemTemplate>
           <asp:Button ID = "button2" runat = "server" CommandName="cmdYourButtonPurpose" CommandArgument='<%# Bind("roc_id") %>'
                Text='<%# Bind("roc_id") %>' />
</ItemTemplate>

代码背后

void ContactsGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
     if(e.CommandName=="cmdYourButtonPurpose")
     {   
         Response.Redirect("http://xxx.yahoo.com/testing/adhoc/Edit.asp?ROCID=" + e.CommandArgument.ToString(););
     }
}
于 2012-11-09T19:17:26.287 回答