i have a datalist to display the details of item from table "PRODUCTS" of my database, in datalist has link button .. on click on linkbutton i want to store the values (name & price ) of that row in the another table "ORDERS ". I am using jquery -ajax method for it .. but on button click the code work fine and values inserted in table "Orders" without any postback , but its happens only once i;e when i click again nothing happens, no values inserted , neither show any error. suggest me some solution or right code to do it .
<asp:DataList ID="DataList1" RepeatColumns="4" RepeatLayout="table"
RepeatDirection="horizontal" runat="server" >
<ItemTemplate>
<table class="item_wrap" id="tbl" >
<tr style=" height:60px">
<td align="center" >
<asp:Image ID="sh1" Height="120px" Width="120px" runat ="server"
ImageUrl='<%#Eval("produrl")%>' CssClass="item_pic" />
</td>
</tr>
<tr style="height:15px; padding-left:20px">
<td >
<asp:Label ID="l1" runat="server" Text='<%#Eval("prodname")%>' Height="25px" CssClass="item_title" ></asp:Label>
</td>
</tr>
<tr style=" height:15px">
<td style="color:#fa7a0a" >
</asp:Label><asp:Label ID="l2" runat="server" Text='<%#Eval("Aprice")%>' CssClass="apirce" >
</asp:Label>
</td>
<td >
<asp:Label ID="l3" runat="server" Text='<%#Eval("productid")%>' Visible="False"></asp:Label>
</td>
</tr>
<tr style="height:30px; padding-left:15px">
<td>
/>
<asp:LinkButton ID="LinkButton2" runat="server"
CssClass="ImageButton1" Height="20px" Width="90px"></asp:LinkButton>
</td>
</tr>
</table>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:DataList>
<script type="text/javascript">
$(document).ready(function() {
$("[id*=LinkButton2]").click( function() {
var price = $("[id*=l2]", $(this).closest(".item_wrap")).html();
var name = $("[id*=l1]", $(this).closest(".item_wrap")).html();
var pageUrl = '<%=ResolveUrl("Default.aspx")%>'
$.ajax({
type: "POST",
url: pageUrl + "/DisplayMessage",
data: "{'name':'" + name + "','price':'" + price + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccessCall,
error: OnErrorCall
});
function OnSuccessCall(response) {
}
function OnErrorCall(response) {
alert(response.status + " " + response.statusText);
}
});
});
</script>
default.aspx.cs
[WebMethod]
public static string DisplayMessage(string name, string price)
{
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["eshopConnectionString"].ConnectionString);
//Creating Delay""
conn.Open();
string sql = string.Format(@"INSERT INTO [eshop].[dbo].[orders]
([name]
,[price])
VALUES
('{0}'
,'{1}')", name, price); SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = sql;
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
conn.Close();
return " success";
}