【发布时间】:2019-08-23 19:34:21
【问题描述】:
我有一个带有自定义按钮的列的网格视图。 当我点击按钮时,会通过 JavaScript 询问用户联系方式,然后将联系方式传递给 gridview 的 rowCommand 事件以进行某些操作。
我将 Hiddenfield 用作 gridview 列,但在设置和获取值时出现问题。
JavaScript 代码:
<script type="text/javascript">
function validateContact()
{
var contact = prompt("Please enter your contact number.");
if (isEmpty(contact) && !isNumber(contact) && contact.length != 10)
alert("Invalid contact.");
return;
else
document.getElementById("hiddenCustomerContact.ClientID").value = contact;
}
</script>
asp 代码:
<asp:GridView ID="gridSearchWorker" runat="server" AutoGenerateColumns="false" OnRowCommand="gridSearchWorker_OnRowCommand" OnRowDataBound="gridSearchWorker_RowDataBound">
<Columns>
<asp:BoundField DataField="WORKER" HeaderText="WORKER" SortExpression="WORKER">
<ItemStyle Width="12%" />
</asp:BoundField>
<asp:TemplateField HeaderText = "" ItemStyle-Width="5%">
<ItemTemplate>
<asp:Button ID="btnGetContact" runat="server" Text="Get Contact" CommandName="GetContact" OnClientClick="validateContact();" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CONTACT" HeaderText="CONTACT" SortExpression="CONTACT">
<ItemStyle Width="12%" />
</asp:BoundField>
<asp:TemplateField HeaderText = "" ItemStyle-Width="5%">
<ItemTemplate>
<asp:HiddenField ID="hiddenCustomerContact" runat="server"/>
</ItemTemplate>
<ItemStyle Width="0.0001%" />
</asp:TemplateField>
</Columns>
</asp:GridView>
后面的c#代码:
protected void gridSearchWorker_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "GetContact")
{
OleDbConnection conn = new OleDbConnection(cs);
//ask for contact and check if any feedback pending
HiddenField hiddenCustomerContact =
e.Row.FindControl("hiddenCustomerContact") as HiddenField; //getting error in this line
string score = hiddenCustomerContact.Value;
int n = Int32.TryParse(score, out val);
string customerContact = "";
//check if any feedback pending
int pendingCount;
if (pendingCount > 0)
{
//ask to give feedback
}
else
{
//show worker contact
}
}
}
【问题讨论】:
-
你用的是
getElementById("hiddenCustomerContact.ClientID"),不应该是getElementById("hiddenCustomerContact")吗? -
好的...我会尝试删除 ClinetID。但错误在后面的代码中。
-
您遇到了什么错误?
-
我认为您需要获得该行。也许this 有帮助?
-
@haldo 应该是
getElementById("<%= hiddenCustomerContact.ClientID %>")来获取webforms中的元素ID。
标签: javascript c#