C# 从客户端调用方法
在下面的代码中,我在onblur事件的网格视图中有一个文本框,我想在服务器端调用一个方法,我试图调用一个方法,但在静态方法中它抛出错误。它的视图状态变为null。如何检索viewstate值?请帮我解决这个问题 Javascript:C# 从客户端调用方法,c#,asp.net,C#,Asp.net,在下面的代码中,我在onblur事件的网格视图中有一个文本框,我想在服务器端调用一个方法,我试图调用一个方法,但在静态方法中它抛出错误。它的视图状态变为null。如何检索viewstate值?请帮我解决这个问题 Javascript: function CallingServerSideFunction() { PageMethods.ToUpper(CallSuccess, CallError); function CallSuccess(re
function CallingServerSideFunction() {
PageMethods.ToUpper(CallSuccess, CallError);
function CallSuccess(res) {
alert(res);
}
function CallError() {
alert('Error');
}
}
代码隐藏:
[System.Web.Services.WebMethod]
public static void ToUpper()
{
AddNewRowToGrid();//throws error how can i call this method?
}
标记:
<asp:ScriptManager ID="newIndentScriptManager" EnablePageMethods="true" runat="server"></asp:ScriptManager>
<asp:TemplateField HeaderText="Quantity" ItemStyle-Width="150px">
<ItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server" Height="20px" Width="150px" onblur="CallingServerSideFunction()" > </asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
private void AddNewRowToGrid()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//extract the TextBox values
DropDownList txtProductName = (DropDownList)gvProduct.Rows[rowIndex].Cells[0].FindControl("ddlProduct");
TextBox txtCurrentStock = (TextBox)gvProduct.Rows[rowIndex].Cells[1].FindControl("txtCurrentStock");
TextBox txtQuantity = (TextBox)gvProduct.Rows[rowIndex].Cells[2].FindControl("txtQuantity");
TextBox txtProductRequiredDate = (TextBox)gvProduct.Rows[rowIndex].Cells[4].FindControl("txtProductRequiredDate");
Label txtUnitType = (Label)gvProduct.Rows[rowIndex].Cells[3].FindControl("lblunittype");
drCurrentRow = dtCurrentTable.NewRow();
dtCurrentTable.Rows[i - 1]["Column1"] = txtProductName.Text;
dtCurrentTable.Rows[i - 1]["Column2"] = txtCurrentStock.Text;
dtCurrentTable.Rows[i - 1]["Column3"] = txtQuantity.Text;
dtCurrentTable.Rows[i - 1]["Column4"] = txtProductRequiredDate.Text;
dtCurrentTable.Rows[i - 1]["Column5"] = txtUnitType.Text;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
gvProduct.DataSource = dtCurrentTable;
gvProduct.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks
SetPreviousData();
}
私有void AddNewRowToGrid()
{
int rowIndex=0;
如果(ViewState[“CurrentTable”]!=null)
{
DataTable dtCurrentTable=(DataTable)视图状态[“CurrentTable”];
DataRow drCurrentRow=null;
如果(dtCurrentTable.Rows.Count>0)
{
对于(inti=1;i您不能像这样从客户端调用服务器方法。您必须使用AJAX包装调用
Javascript已经有了一个ToUpper方法
ViewState在webmethods中不可用-ViewState仅在网页发生回发且ViewState隐藏字段可用时才可用
WebMethods不要求发布ViewState,也不创建页面对象的实例。因此,您需要另一种机制,如会话(尽管这有其自身的问题,尤其是在负载平衡环境中)在ViewState中存储当前所依赖的数据。如果您的任务是将该文本框中的所有文本大写,那么有更简单的方法来实现这一点
方法1
这是最简单的,因为它不需要编码,只需要CSS规则。在标题中,添加以下内容:
<head runat="server">
<style type="text/css">
.upper {
text-transform: uppercase;
}
</style>
</head>
<script type="text/javascript">
$("#<%= GridView1.ClientID %> input[id*='txtQuantity']").focusout(function() {
$(this).val($(this).val().toUpperCase());
});
</script>
我不知道网格视图的名称是什么,但是用网格视图的名称替换GridView1
。如果您有其他需要大写的文本框,只需复制并粘贴此代码,然后用文本框的id替换txtQuantity
。Viola!我已经纠正了它,我只能调用视图tate值变为Null我完全不明白您是如何调用它的…您的意思是在浏览器中单击文本框,然后从文本框中出来,然后看到错误?另外,AddNewRowToGrid()的代码在哪里?文本框在网格视图中。我是说,实际错误在AddNewRowToGrid()中;有一个视图状态变为null。这是因为静态方法吗?我不知道。发布AddNewRowToGrid()的代码。ViewState为null还是ViewState[“CurrentTable”]为null?这是两个完全不同的东西。你在哪里设置ViewState[“CurrentTable”]?Hi Touper是一个方法,我不会将文本转换为上层类
<script type="text/javascript">
$("#<%= GridView1.ClientID %> input[id*='txtQuantity']").focusout(function() {
$(this).val($(this).val().toUpperCase());
});
</script>