2012-02-21 140 views
0

使用C#,.NET,Visual Studio 2010中填充页面重定向外部URL

我有一个情况,当navigationurl应该使用步骤 1调用Web服务和动态创建的GridView控件上的超链接,用户点击传递少量值并接收需要附加到控件的导航网址的新idkey,我可以在RowDataBound事件下完成此操作,但它会对网格中的每一行进行Web服务的浪费调用。相反,我希望在用户点击链接时完成此操作?

我想到的另一个想法是在填充页面之间使用页面来重定向,但需要自动提交?任何线索在这?或更好的方式?

感谢

 <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); 
+0

什么是C#.net 10? – Polity 2012-02-21 13:37:10

+0

@Polity大概意思是视觉工作室2010 – Aristos 2012-02-21 13:50:23

+0

@Polity是VS 2010 – Gauls 2012-02-21 13:58:03

回答

0

删除datanavigation代码

string[] dataNavigateUrlFields = {"link"};    LinksBoundField.DataTextField = "link";    LinksBoundField.DataNavigateUrlFields = dataNavigateUrlFields; 

变更为以下网址与grdviewLink.NavigateUrl传递没有得到错杂。

<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); 

       } 
      } 
     } 

Navigate.ashx文件

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()); 
      } 

这个作品非常好

由于科特使用了ashx的文件,而不是ASPX从这里尖

ashx

2

当然,你可以采取一种方法是使用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> 

和USI ng 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> 

,关键是要利用适当的Jquery selectors获取你的链接,并通过任何参数传递合适(在我们使用隐藏的输入和DOM导航上面的例子...

+0

哪里是'点击'的HTML? bind('click',function()?问题是读取每个gridview行和单元格以获取参数值,并且一旦接收到新密钥,我必须将它附加到超链接url。这听起来在jquery中非常复杂,我从不使用em。 – Gauls 2012-02-21 14:44:31

+0

'click'是Javascript的鼠标事件,没有必要通过它们链接每个gridview行或循环(JQuery选择器为你做),并且不需要追加URL,因为重定向可以发生在'client -side' 尝试将上面的代码复制到一个新的HTML页面中,看看它是如何工作的如果你不喜欢Javascript和Jquery,那么你可能想看看运行客户端代码的能力和优势,方。 – SeanCocteau 2012-02-21 15:03:30

+0

@ SeanCocteau:感谢解释会试试看。 – Gauls 2012-02-21 15:18:59