我有一个gridview来显示表中的请求信息。 我添加了一个按钮模板,它在点击后会将用户带到相应的请求URL
如何捕获所选的请求并将其作为重定向URL中的变量传递
我的gridview
<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是我需要传递给网址的变量
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect("http://xxx.yahoo.com/testing/adhoc/Edit.asp?ROCID=roc_id");
}
答案 0 :(得分: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);
}
答案 1 :(得分:0)
您需要RowCommand事件以及 CommandArgument 才能在RowCommand事件中传递值。
<强> 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(););
}
}