2017-03-08 56 views
-1

例如,现在我有我的ASPX像这样:如何从我的aspx页面将参数传递到C#代码隐藏方法?

... 
<tr> 
    <td class="label"> 
     Start Date: 
    </td> 
    <td> 
     <input type="text" name="StartDate" value="<%=GetCurrentDate()%>" maxlength="10" /> <div class="format"><i>(format: mm/dd/yyyy)</i></div> 
    </td> 
</tr> 
... 

..和我的C#如下:

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

这工作得很好。但是,如果我想从ASPX端传入参数呢?就像这样:

... 
<tr> 
    <td class="label"> 
     Start Date: 
    </td> 
    <td> 
     <input type="text" name="StartDate" value="<%=GetCurrentDate("parameter here")%>" maxlength="10" /> <div class="format"><i>(format: mm/dd/yyyy)</i></div> 
    </td> 
</tr> 
... 

-

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

任何帮助,这将不胜感激。

+0

当你尝试时会发生什么? 'GetCurrentDate(“parameter here”)' – maksymiuk

+0

<%= GetCurrentDate(“testParam”)%>返回:03/08/2017Ae27APara31 –

回答

1

要格式化输出日期错单引号括起来的属性值。 请参考下面的例子:

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

的 “+ VAL” 为ToString方法之外。 如果在val中有令牌参数,则它们将更改为DateTime格式标记,并且输出是您发布的内容。

希望这可以帮助。

+0

我不能相信我错过了。这解决了我的问题。非常感谢! –

0

字符串必须用在C#双引号,但你可以在HTML

+0

双引号不会改变任何内容。我会编辑原始问题以使其更清楚。 <%= GetCurrentDate(“testParam”)%>返回:03/08/2017Ae27APara31 –

+0

那么,你问如何调用函数,而不是如何格式化日期:) –

相关问题