Thursday, March 3, 2011

pass "calendar.selectedvalue" in querystring from gridview

I wanted to pass "calendar1.Selecteddate" in a query string from gridview in one page to another gridview (I have written sqlquery in that gridview) in another page. As seen in the below code I tried passing it but this did not work. Can anyone tell me how to pass the selected date from calendar in query string

      <asp:HyperLinkField DataNavigateUrlFields="LocalIP" 
                DataNavigateUrlFormatString="DailyResults.aspx?
           Terms={0}&column=LocalIP&    
               startdate=Calendar1.SelectedDate.Date.Date.ToShortDateString()
                DataTextField="LocalIP" HeaderText="User" />
From stackoverflow
  • In the example you've provided it would treat that calendar part of the string as a literal and pass the exact value you have typed. In order to obtain the data using you would need to do something similar to:

    <asp:HyperLinkField DataNavigateUrlFormatString="<%=GetSelectedDate()%>....
    

    where this is a method on the code behind class (or you could use a property) that creates the string you intend, including the {0} placeholder for the LocalIP bound data field.

  • I am assuming the calendar selected value has not changed since the gridview was rendered.

    <asp:HyperLinkField DataNavigateUrlFields="LocalIP" 
         DataNavigateUrlFormatString='<%# "DailyResults.aspx?Terms={0}&column=LocalIP&startdate=" + Calendar1.SelectedDate.Date.Date.ToShortDateString() %> DataTextField="LocalIP" HeaderText="User" />
    
  • HectorMac,

    Ur code did not work. Its giving me the following error:

    Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

    Parser Error Message: Databinding expressions are only supported on objects that have a DataBinding event. System.Web.UI.WebControls.HyperLinkField does not have a DataBinding event.

  • One solution is to build your URL string in the code-behind, instead of building it in the markup.

    Override the RowDataBound method on the GridView and build the hyperlink programmatically:

    protected override gv1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
      HyperLink hl = new Hyperlink();
      hl.NavigateUrl = string.Format("DailyResults.aspx?Terms={0}&column=LocalIP&startdate={1}", localIp, Calendar1.SelectedDate.Date.Date.ToShortDateString());
      .. set other hyperlink fields ..
      e.Row.Cells[1].Controls.Add(hl);
    }
    

    Hope that helps!

0 comments:

Post a Comment