我正试图通过onclientclick事件将javascript变量值传递给隐藏的feild,后面是代码。我有按钮,同时具有客户端和服务器端onclick事件
<asp:Button ID="btnSave" runat="server" Text="Save" class="buttonstyle"
onclick="btnSave_Click" OnClientClick="otherdata()" />
我想从OnClientClick事件获取代码隐藏中的javascript值,然后想要从Onclick事件中将该值插入数据库表
function otherdata() {
var hv = $('input[id$=hdnOthers]').val();
var $arrT = $('#<%=gv_Others.ClientID %>').find('input:text[id$="txtEmp"]');
var count = [];
for (var i = 0; i < 10; i++) {
var $txt = $arrT[i];
count[i] = $($txt).val();
}
hv = count;
alert(hv);
}
我在警报中获取值并将值分配给隐藏字段,但问题是我没有从隐藏字段中获取隐藏字段中的值...虽然我已经将值从javascript传递给隐藏字段所以为什么我我没有从代码背后获得这个价值..
protected void Insert_OtherServices()
{
dsJobCardTableAdapters.Select_OtherServiceTableAdapter dsother = new dsJobCardTableAdapters.Select_OtherServiceTableAdapter();
string hdn = hdnOthers.Value;
dsother.Insert_OtherService(hdn);
}
答案 0 :(得分:2)
您没有将值分配给隐藏字段。像这样调整你的javascript:
function otherdata() {
var hvField = $('input[id$=hdnOthers]');
var $arrT = $('#<%=gv_Others.ClientID %>').find('input:text[id$="txtEmp"]');
var count = [];
for (var i = 0; i < 10; i++) {
var $txt = $arrT[i];
count[i] = $($txt).val();
}
// do notice you have an array that you assign
// but you don't say the value of count is wrong for you
// so I only fix the bug that you don't assign the value
// to the hidden field
hvField.val(count); // set the field to the value of Count
alert(count);
}
答案 1 :(得分:1)
为此你必须使用Jquery AJAX方法。
喜欢这个
function otherdata() {
var hv = $('input[id$=hdnOthers]').val();
var $arrT = $('#<%=gv_Others.ClientID %>').find('input:text[id$="txtEmp"]');
var count = [];
for (var i = 0; i < 10; i++) {
var $txt = $arrT[i];
count[i] = $($txt).val();
}
$.ajax({
type: "POST",
url: "YourASPXPage.aspx/Insert_OtherServices",
data: {countVal : count},
contentType: "application/json; charset=utf-8",
dataType: "json",
}).done(function(response) {
//alert(response.d);
alert("Data saved succesfully");
});
//alert(hv);
}
现在代码隐藏文件中的方法看起来像这样,我的意思是将其转换为页面方法,用&#34; WebMethod&#34;属性......
[WebMethod()]
public static string Insert_OtherServices(string countVal)
{
dsJobCardTableAdapters.Select_OtherServiceTableAdapter dsother = new dsJobCardTableAdapters.Select_OtherServiceTableAdapter();
string val = dsother.Insert_OtherService(countVal);
return val;
}