Reputation: 27
In the below code i have a textbox inside grid view on onblur event i want to call a method in server side i tried to call a method but in static method it throws error.It has a view state which becomes null.How to retrieve the viewstate value?Pls help me to solve the issue.
Javascript:
function CallingServerSideFunction() {
PageMethods.ToUpper(CallSuccess, CallError);
function CallSuccess(res) {
alert(res);
}
function CallError() {
alert('Error');
}
}
Codebehind:
[System.Web.Services.WebMethod]
public static void ToUpper()
{
AddNewRowToGrid();//throws error how can i call this method?
}
Markup:
<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();
}
Upvotes: 0
Views: 702
Reputation: 48736
If your mission is to capitalize all the text in that textbox, there are much easier ways to do this.
Method 1
This is the easiest because it requires no coding, just a CSS rule. In your head
section, add this:
<head runat="server">
<style type="text/css">
.upper {
text-transform: uppercase;
}
</style>
</head>
And in your template field, change your line to this:
<asp:TextBox ID="txtQuantity" CssClass="upper" runat="server" Height="20px" Width="150px" onblur="CallingServerSideFunction()" > </asp:TextBox>
If you have other text boxes that also need capitalizing, just add that upper
class to it and viola!
Method 2
Whereas Method 1 will capitalize the letters as the user types, method 2 will capitalize the letters after the user tabs away. You will need jQuery, if your project doesn't already have it. At the bottom of your aspx page, before the closing body
tag, add this:
<script type="text/javascript">
$("#<%= GridView1.ClientID %> input[id*='txtQuantity']").focusout(function() {
$(this).val($(this).val().toUpperCase());
});
</script>
I don't know what the name of your Grid View is, but replace the GridView1
with whatever the name of your grid view is. If you have other text boxes that need capitalizing, just copy and paste this code and replace txtQuantity
with whatever the id of the TextBox is. Viola!
Upvotes: 1
Reputation: 173
ViewState isn't available inside webmethods - ViewState is only available when a postback occurs from the webpage and the VIEWSTATE hidden field is available.
WebMethods don't require the ViewState to be POSTed and they dont create an instance of a Page object. So you'll need another mechanism such as Session (although this has its own issues especially in load balanced environemnts) to store the data you're currently relying on in ViewState.
Upvotes: 0
Reputation: 11308
You can't call server methods from the client like this. You would have to wrap the call using AJAX.
Javascript already has a ToUpper method...
http://www.w3schools.com/jsref/jsref_touppercase.asp
Upvotes: 0