0

For example, right now I have my ASPX like so:

<form name="form" runat="server" onsubmit="validate(this)">
    <table width="100%" height="100%">
        <tr>
            <td class="label">
                Start Date:
            </td>
            <td>
                <input type="text" name="StartDate" value='<%=GetCurrentDate("- testParam")%>' maxlength="10" /> 
            </td>
        </tr>
    </table>
</form>

..and my C# as follows:

public static string GetCurrentDate(string str)
{
    return DateTime.Now.ToString("MM/dd/yyyy") + str;
}

This works fine and outputs "03/08/2017 - testParam". But what if, for example, instead of sending a hardcoded string manually as I did above, I want to pass in one of the HTML element's values as a parameter from the ASPX side? Like this:

...
<tr>
    <td class="label">
        Start Date:
    </td>
    <td>
        <input type="text" name="StartDate" value="<%=GetCurrentDate(formObj.elements.item('someLabel').value)%>" maxlength="10" />
    </td>
</tr>
...

What do I need to do to get the "someLabel" element's value on my ASPX page to the C# page? Any help with this will be greatly appreciated.

8
  • I can't even tell from these snippets whether your input is in a form. If it isn't, it'll never be posted back to the page. Commented Mar 8, 2017 at 18:26
  • You could also look into whether it's feasible to use javascript to post the values via AJAX. I don't recall how hard it is to do that with an ASPX form postback. Commented Mar 8, 2017 at 18:28
  • The approach is really odd in the sense of ASP.Net Web Form. What are you hoping to achieve? Commented Mar 8, 2017 at 18:29
  • @maniak1982 - Added form element to snippet. Commented Mar 8, 2017 at 18:32
  • @Win I need to pass user-selected parameters from certain front-end UI elements to the C# code-behind such that I can perform logic on them and return data accordingly. Commented Mar 8, 2017 at 18:33

4 Answers 4

1

If you want to pass a value from client-side to code behind without posting back the entire page, you will need to use Ajax.

Calling to a server-side method in ASP.Net Web Form is not as clean as ASP.Net Web API or MVC. You will need to use old WebMethod.

For example,

enter image description here

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DemoWebForm.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <button type="button" onclick="getData();">Get Data</button>
        <br/>
        <input type="text" name="StartDate" id="txtStartDate" maxlength="10" />
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <script type="text/javascript">
            function getData() {
                var data = {value: "test"};
                $.ajax({
                    type: "POST",
                    url: '<%= ResolveUrl("~/Default.aspx/GetCurrentDate") %>',
                    data: JSON.stringify(data),
                    contentType: "application/json",
                    success: function (msg) {
                        $("#txtStartDate").val(msg.d);
                    }
                });
            }
        </script>
    </form>
</body>
</html>

Code Behind

using System;
using System.Web.Script.Serialization;

namespace DemoWebForm
{
    public partial class Default : System.Web.UI.Page
    {
        [System.Web.Services.WebMethod]
        public static string GetCurrentDate(string value)
        {
            return new JavaScriptSerializer().Serialize(
                string.Format("{0} - {1}", DateTime.Now, value));
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

So this solution is the old methodology to complete what I'm trying to do? And the ASP.NET Web API / MVC is best practice as of today?
Forms are are older technology but there's nothing inherently wrong in using them. ASP.NET MVC/Web API can be a lot easier for modern web design, though, since it makes it easier to develop the front end separately from the back end.
1

this method GetCurrentDate running server side but this formObj.elements.item('someLabel').value running on client

try this..

<tr>
<td class="label">
    Start Date:
</td>
<td>
    <input type="text" name="StartDate" value='<%=GetCurrentDate()%>' maxlength="10" /> 
</td>

    public  string GetCurrentDate()
    {
        return DateTime.Now.ToString("MM/dd/yyyy");
    }

for read value of input named as StartDate from server..

string postValue =  Request.Form["StartDate"]

2 Comments

"An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Request.get'."
change public static string GetCurrentDate() to public string GetCurrentDate()
1

You can make it a server control by adding runat="server" and it will be available in your code behind file. OR if you don't prefer this then use Request.Form["Name"] in your code behind file. Here "Name" is the name you are giving to your textbox control.

In your case the name is StartDate

So try to access the value of textbox from the code behind using Request.Form["StartDate"]

Read this article.. https://www.aspsnippets.com/Articles/Get-value-of-HTML-Input-TextBox-in-ASPNet-code-behind-using-C-and-VBNet.aspx

Comments

0

You should post value you want to send to the webserver into an html form.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.