2012-02-21 141 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