C# 用于重定向外部url的填充页
使用c#,.net,Visual Studio 2010 我有一种情况,当用户点击gridview上的超链接时,导航URL应该使用步骤动态创建 1调用webservice并传递几个值并接收需要附加到控件导航url的新idkey,我可以在RowDataBound事件下完成此操作,但它会对网格中的每一行对webservice进行浪费调用。相反,我希望在用户单击链接时完成此操作 我能想到的另一个想法是在填充页面之间使用一个页面,称为重定向,但这需要自动提交??有线索吗???还是更好的方法 谢谢C# 用于重定向外部url的填充页,c#,asp.net,c#-4.0,C#,Asp.net,C# 4.0,使用c#,.net,Visual Studio 2010 我有一种情况,当用户点击gridview上的超链接时,导航URL应该使用步骤动态创建 1调用webservice并传递几个值并接收需要附加到控件导航url的新idkey,我可以在RowDataBound事件下完成此操作,但它会对网格中的每一行对webservice进行浪费调用。相反,我希望在用户单击链接时完成此操作 我能想到的另一个想法是在填充页面之间使用一个页面,称为重定向,但这需要自动提交??有线索吗???还是更好的方法 谢谢
<asp:GridView ID="GridViewLinkedService" runat="server" AutoGenerateColumns="False" DataKeyNames = "applicationId"
DataSourceID="ObjectDataLinkedService" Height="213px" Width="897px">
<Columns>
<asp:BoundField DataField="applicationId" HeaderText="Application ID" Visible ="true"
SortExpression="applicationId">
<HeaderStyle BackColor="#4B6C9E" BorderStyle="Solid" Font-Bold="True"
Font-Names="Verdana" Font-Size="Small" ForeColor="White" />
<ItemStyle Width="10px" />
</asp:BoundField>
<asp:BoundField DataField="referenceNumber" HeaderText="Reference Number"
SortExpression="referenceNumber" >
<HeaderStyle BackColor="#4B6C9E" BorderStyle="Solid" Font-Bold="True"
Font-Names="Verdana" Font-Size="Small" ForeColor="White" />
<ItemStyle Width="30px" />
</asp:BoundField>
<asp:BoundField DataField="applicationName" HeaderText="Application Name"
SortExpression="applicationName" >
<HeaderStyle BackColor="#4B6C9E" BorderStyle="Solid" Font-Bold="True" Font-Names="Verdana" Font-Size="Small" ForeColor="White"/>
</asp:BoundField>
<asp:BoundField DataField="address" HeaderText="Address"
SortExpression="address" >
<HeaderStyle BackColor="#4B6C9E" BorderStyle="Solid" Font-Bold="True" Font-Names="Verdana" Font-Size="Small" ForeColor="White" />
</asp:BoundField>
</Columns>
Hyperlink column is added in the Pageload
HyperLinkField LinksBoundField = new HyperLinkField();
string[] dataNavigateUrlFields = {"link"};
LinksBoundField.DataTextField = "link";
LinksBoundField.DataNavigateUrlFields = dataNavigateUrlFields;
**LinksBoundField.NavigateUrl; //call webservice(send appid and refno) and append newtoken to url (reieved from datasource link)**
LinksBoundField.DataNavigateUrlFormatString = "http://" + Helper.IP + "/" + Helper.SiteName + "/" + Helper.ThirdPartyAccess + "?value={0}&token=" + Session["Token"];
LinksBoundField.HeaderText = "Link";
LinksBoundField.Target = "_blank";
GridViewLinkedService.Columns.Add(LinksBoundField);
GridViewLinkedService.RowDataBound += new GridViewRowEventHandler(grdView_RowDataBound);
超链接列添加到页面加载中
HyperLinkField LinksBoundField=新的HyperLinkField();
字符串[]dataNavigateUrlFields={“链接”};
LinksBoundField.DataTextField=“link”;
LinksBoundField.DataNavigateUrlFields=DataNavigateUrlFields;
**LinksBoundField.NavigateUrl//调用webservice(发送appid和refno)并将newtoken附加到url(从数据源链接获取)**
LinksBoundField.DataNavigateUrlFormatString=“http://”+Helper.IP+“/”+Helper.SiteName+“/”+Helper.ThirdPartyAccess+”?值={0}&token=“+Session[“token”];
LinksBoundField.HeaderText=“Link”;
LinksBoundField.Target=“_blank”;
GridViewLinkedService.Columns.Add(LinksBoundField);
GridViewLinkedService.RowDataBound+=新的GridViewRowEventHandler(grdView_RowDataBound);
当然,您可以采取的一种方法是使用Jquery在客户端生成链接,而不需要填充页
例如,下表有一些虚拟锚标记
<table cellspacing="0" border="1" id="grdSpys">
<tbody>
<tr>
<th align="center" scope="col">Name<th align="center" scope="col">Action</th>
</tr>
<tr>
<td>Mr A</td>
<td>
<a href="#">Click Me</a>
<input type="hidden" value="Anthony" />
</td>
</tr>
<tr>
<td>Mr B</td>
<td>
<a href="#">Click Me</a>
<input type="hidden" value="Barry" />
</td>
</tr>
<tr>
<td>Mr C</td>
<td>
<a href="#">Click Me</a>
<input type="hidden" value="Carl" />
</td>
</tr>
<tr>
<td>Mr D</td>
<td>
<a href="#">Click Me</a>
<input type="hidden" value="Don" />
</td>
</tr>
<tr>
<td>Mr E</td>
<td>
<a href="#">Click Me</a>
<input type="hidden" value="Ethan" />
</td>
</tr>
</tbody>
命名动作
A先生
B先生
C先生
D先生
E先生
通过使用Jquery,您可以绑定所有这些链接以执行一个特定操作,即转到Web服务
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#grdSpys a").bind('click' , function() {
var secretName = $(this).next().val();
alert('goto the webservice, tell them it is ' + secretName );
});
});
</script>
$(文档).ready(函数(){
$(“#grdSpys a”).bind('click',function(){
var secretName=$(this.next().val();
警报('转到Web服务,告诉他们它是'+secretName');
});
});
诀窍是利用适当的方法获取链接,并根据需要传递任何参数(在上面的示例中,我们使用隐藏输入和DOM导航…删除了数据导航代码
string[] dataNavigateUrlFields = {"link"}; LinksBoundField.DataTextField = "link"; LinksBoundField.DataNavigateUrlFields = dataNavigateUrlFields;
更改为下面的url,通过grdviewLink.NavigateUrl传递的url不会混乱
<asp:HyperLinkField DataTextField="link" HeaderText="Multi-Link" Target="_blank" ItemStyle-Width = "5px" ItemStyle-Wrap ="true">
<HeaderStyle Wrap="True" Width="5px" BackColor="#4B6C9E" BorderStyle="Solid" Font-Bold="True" Font-Names="Verdana" ForeColor="White"/>
</asp:HyperLinkField>
protected void grdView_RowDataBound(object sender, GridViewRowEventArgs e)
{
string strvalue = "";
string strRef = "";
string strAppId = "";
foreach (GridViewRow row in GridViewLinkedService.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{ //reference and appid
strAppId = row.Cells[0].Text;
strRef = row.Cells[1].Text;
HyperLink grdviewLink = (HyperLink)row.Cells[6].Controls[0];
strvalue = grdviewLink.Text;
grdviewLink.NavigateUrl = "~/My Service/Navigate.ashx?AppID=" + strAppId.ToString() + "&Ref=" + strRef.ToString() + "&nurl=" + Server.UrlEncode(strvalue);
}
}
}
这很有效
多亏了Curt,我从这里得到了使用ashx文件而不是aspx的窍门
@Polity可能指的是visual studio 2010,html?绑定中的“单击”在哪里(“单击”,函数()?问题是读取每个gridview行和单元格以获取参数值,一旦收到newkey,我就必须将其附加到超链接url。在jquery中,这听起来很复杂,我从不使用它们。“单击”是Javascript鼠标事件。不需要链接每个gridview行或循环它们(jquery选择器为您执行此操作)无需附加URL,因为重定向可能发生在“客户端”。尝试将上述内容复制到一个新的HTML页面中,看看它是如何工作的。如果您与Javascript和Jquery不相似,那么您可能希望查看针对服务器端运行客户端代码的功能和优势。@SeanCocteau:我猜是javas由于webservice调用需要一个usernametoken和多个输入,所以脚本将没有用处。
public void ProcessRequest(HttpContext context)
{
if (context.Request.QueryString.GetValues("AppID") != null)
{
appid = context.Request.QueryString.GetValues("AppID")[0].ToString();
}
if (context.Request.QueryString.GetValues("Ref") != null)
{
refno = context.Request.QueryString.GetValues("Ref")[0].ToString();
}
if (context.Request.QueryString.GetValues("nurl") != null)
{
nurl = HttpUtility.UrlDecode(context.Request.QueryString.GetValues("nurl")[0].ToString());
}