Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
There is two calendars , one drop down and one grid view and button. Now i try to access these ID's on server side

What I have tried:

ASP.NET
 <asp:DropDownList ID="regiondrop" runat="server" AutoPostBack="True" 
    onselectedindexchanged="regiondrop_SelectedIndexChanged">
    </asp:DropDownList>
 <input  ID="fromdate" value="dd/mm/yyyy" runat="server" clientidmode="static" />
  <input  ID="todate" value="dd/mm/yyyy" runat="server" clientidmode="static" />
<input type="button" id="search_data"    class="sear_btn"  value="Search Data"  />
and gridview  


now i try to display grid view on page

for this i try this

JavaScript
<script type="text/javascript">
$(function () {

        var fromdate = $('[ID*=fromdate]').val();
        var todate = $('[ID*=todate]').val();
        var regiondrop = $('[ID*=regiondrop] option:selected')[0].value;
        var GridView1 = $('[ID*=GridView1]');
        var obj = {};
        obj.fromdate = todate ;
        obj.todate = todate ;
        obj.regiondrop = regiondrop ;
        obj.GridView1 =GridView1 ;
        Getdataa(obj);
        return false;

});
function Getdataa(obj) {
    //alert('1');
    $.ajax({
        type: "POST",
        url: "WebForm1.aspx/search_data",
        data: "{'fromdate':'" + fromdate + "','todate':'" + todate + "','regiondrop':'" + regiondrop + "','GridView1':'" + GridView1 + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: true,
        cache: false,
        success: function (result) {
            $("#GridView1").empty();
            if(data.d.length>0){
            $("#GridView1").append(
            "<tr><th>OName</th><th>RegNo</th>><th>Speed</th>");

            for(var i=0;i<data.d.length;i++){
            $("#GridView1").append("<tr><td>" +
            data.d[i].OName + "</td> <td>" +
            data.d[i].RegNo + "</td> <td>" +
            data.d[i].Speed + "</td></tr>");
               }
           }
        },
        error: function (error) {
            alert("error");

        }
    });
}


For access ID's i try to get ID and then i pass these ID in webmethod function like ths
C#
[WebMethod]
   public static string search_data(DateTime fromdate, DateTime todate, string regiondrop)
   {

       try
       {

           DateTime frmdate =
          Convert.ToDateTime(fromdate.Value.Trim().Split('T')[0]);
           DateTime tdatee =
           Convert.ToDateTime(todate.Value.Trim().Split('T')[0]);
           string regionvalue = Convert.ToString(regiondrop.SelectedValue);
           TrackDataEntities1 ts = new TrackDataEntities1();

           // here dq code is like this var dq= and here i write LINQ query
           //i don't paste because of long query
           GridView1.DataSource = dq;
           GridView1.DataBind();

       }
       catch (Exception)
       {
           GridView1.Visible = false;
           Label4.Text = ("No Data");

       }

   }


when i try this show errors

Error 2 'System.DateTime' does not contain a definition for 'Value' and no extension method 'Value' accepting a first argument of type 'System.DateTime' could be found (are you missing a using directive or an assembly reference?)

Error 3 'System.DateTime' does not contain a definition for 'Value' and no extension method 'Value' accepting a first argument of type 'System.DateTime' could be found (are you missing a using directive or an assembly reference?)

Error 4 'string' does not contain a definition for 'SelectedValue' and no extension method 'SelectedValue' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)

Error 5 An object reference is required for the non-static field, method, or property 'chart_project.WebForm1.GridView1'

Error 8 An object reference is required for the non-static field, method, or property 'chart_project.WebForm1.Label4'

any solution
Posted
Updated 13-Jul-16 22:22pm

1 solution

You're not passing control references (eg textbox, dropdownlist etc), just simple types like DateTime and string. So you're only getting the values of what is in the controls, not the controls themselves.

C#
DateTime frmdate =
          Convert.ToDateTime(fromdate.Value.Trim().Split('T')[0]);


There is no point converting fromdate to datetime as it is already datetime, if you only want the date part use

C#
DateTime frmdate = fromdate.Date;


You might also have conversion problems in your code as you are expecting .net to be able to convert whatever you ass as the fromdate param to DateTime and it might not be able to due to regional variations. You could make your fromdate param a string instead and do the conversion to DateTime in the webmethod itself using DateTime.TryParseExact. That way you can more gracefully handle conversion errors.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900