TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Terry
NA
148
0
Call Server Side method dynamically added HTML button Ajax
Apr 2 2015 6:37 AM
Hi,
In my asp:Table present in aspx, am adding buttons from code-behind dynamically. I am trying to call Server side Method on click of the button using Ajax.
Here's the code of buttons added dynamically :
foreach (UnitPattern up in fp.UnitPattern)
{
System.Diagnostics.Debug.WriteLine("UNIT PATTERN : PlanType : " + up.UnitPlanType.UnitPlanName + " Direction : " + up.UnitDirection.DirectionTitle + " STATUS : " + up.Unit_Status + " UNIT NO : " + up.UnitNo);
tc = new TableCell();
LiteralControl lc = null;
sb = new System.Text.StringBuilder();
sb.Append("<input type=\"button\" onclick='javascriptmethodname();' name=\"");
sb.Append(up.UnitPatternId.ToString() + "\" "); // Add value for ID
sb.Append(" text=\"");
sb.Append(up.UnitNo + "\" value=\"");
sb.Append(up.UnitNo + "\"");
sb.Append(" style=\"background:");
if (up.Unit_Status == UnitStatus.Available)
{
sb.Append("green;\" />");
lc = new LiteralControl(sb.ToString());
tc.Controls.Add(lc);
}
else if (up.Unit_Status == UnitStatus.Block)
{
sb.Append("blue;\" />");
lc = new LiteralControl(sb.ToString());
tc.Controls.Add(lc);
}
else if (up.Unit_Status == UnitStatus.Sold)
{
sb.Append("red;\" />");
lc = new LiteralControl(sb.ToString());
tc.Controls.Add(lc);
}
System.Diagnostics.Debug.WriteLine("SB = " + sb.ToString());
tr.Controls.Add(tc);
}
The result of generated sb with this code is :
SB = <input type="button" onclick='javascriptmethodname();' name="7" text="6" value="6" style="background:red;" />
In my aspx, the script code :
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<script type ="text/javascript">
function javascriptmethodname() {
$.ajax({
type: "POST",
url: "ProjectSelectForm.aspx/Foo",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: "{a: \"webmethod!\"}",
cache: "false",
async: "true",
success: function (data) {
alert(data.d);
},
error: function () {
alert("error");
}
});
}
</script>
Finally the server side method to be called :
public static string Foo(string a)
{
System.Diagnostics.Debug.WriteLine("######## RCVD in WebMethod Para = " + a);
return a;
}
With the above code, on clicking a button, I get a message box with msg "undefined". and the Foo() of server side is never executed.
I want is that the ID/name of the button to be passed to the Foo(), as that is the ID of a model. Based on that ID value I can retrieve object from DB and thus move ahead.
Can you help me identify why I am getting this undefined msg & what am I missing here !!
any help is highly appreciated.
FYI, I have tried a lot using simple method i.e. adding a asp Button & setting it's Click event, but that doesn't work at all. Here are the links for that question - http://www.c-sharpcorner.com/Forums/Thread/291647/button-click-in-a-tablecell-never-fires-and-table-disappears.aspx
http://www.c-sharpcorner.com/Forums/Thread/290626/problem-with-dynamically-added-rows-with-buttons-to-table.aspx
So now, am trying out this way.
Please help me, am stuck with this issue since long and not able to solve it yet.
Reply
Answers (
2
)
asp.net Web Api
choosing programming model in asp.net