简体   繁体   中英

filler page for redirecting external url

Using c#, .net, Visual Studio 2010

I have a situation when a user clicks on Hyperlink on the gridview the navigationurl should be created on the fly using steps 1 calling webservice and passing few values and receive new idkey which needs to be attached to the navigation url of the control, i can get this done under RowDataBound event, but it will make waste calls to webservice for each row in the grid. Instead i want this to be done when the user clicks the link??

another idea i can think of is using a page in between fill page saying redirecting, but that needs to be auto submit?? any clue on this??? or better way of doing??

Thanks

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

Certainly one approach you can take is to generate the links on the client-side using Jquery, not need for the filler page...

For instance, the following table has some dummy anchor tags...

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

and by using Jquery, you can bind all those links to perform one specific action, ie goto a webservice.

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

the trick is to utilise the appropriate Jquery selectors to obtain your links and passing through any parameters as appropriate (in the above example we use hidden inputs and DOM navigation...

deleted the datanavigation code

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

Changed to below url passed with grdviewLink.NavigateUrl does not get jumbled up.

 <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 file

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

this works very well

Thanks to Curt got the tip of using ashx file instead of aspx from here

ashx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM