簡體   English   中英

重定向外部網址的填充頁面

[英]filler page for redirecting external url

使用c#、. net,Visual Studio 2010

我有一種情況,當用戶單擊gridview上的Hyperlink時,應該使用第1步調用webservice並傳遞一些值並接收需要附加到控件的導航url的新idkey來動態創建Navigationurl。這是在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);

當然,您可以采用的一種方法是使用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>

通過使用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選擇器來獲取您的鏈接,並根據需要傳遞任何參數(在上面的示例中,我們使用隱藏的輸入和DOM導航...

刪除了數據導航代碼

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

更改為以下通過grdviewLink傳遞的url。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());
            }

這很好

感謝Curt從這里獲得了使用ashx文件而不是aspx的提示

阿什克斯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM