通过Javascript将值从Gridview传递到Hiddenfield返回null
本文关键字:Hiddenfield 返回 null Gridview Javascript 通过 | 更新日期: 2023-09-27 18:06:01
我正在像下面这样传递一个字段值给Javascript。
<asp:TemplateField>
<ItemTemplate>
<a href="JavaScript:divexpandcollapse('div<%# Eval("ClaimMasterId") %>');">
<img id='imgdiv<%# Eval("ClaimMasterId") %>' width="9px" border="0" src="Img1/plus.gif" alt="" title="Add Action Notes" /></a>
</ItemTemplate>
<ItemStyle Width="10px" VerticalAlign="Middle"></ItemStyle>
</asp:TemplateField>
Javascript: <script type="text/javascript">
function divexpandcollapse(divname) {
debugger;
var div = document.getElementById(divname);
document.getElementById('<%= HiddenField1.ClientID %>').value = div;
var img = document.getElementById('img' + divname);
if (div.style.display == "none") {
div.style.display = "inline";
img.src = "Img1/minus.gif";
} else {
div.style.display = "none";
img.src = "Img1/plus.gif";
}
}
</script>
我得到隐藏字段值后面的代码像这样:
protected void ClearImageButton_Click(object sender, ImageClickEventArgs e)
{
string s = HiddenField1.Value;
}
假设,ClaimMasterId值为4507,然后在IE中调试Javascript时,我注意到传递值是'div4507',但在代码中返回空值。我犯错误的地方。谁能给我指路?
实际上,我使用gridview与扩展折叠函数使用javascript。不使用SelectedIndexChanged事件,我试图使用javascript获得行值。'ClaimMasterId'是每行的键值,所以如果我在用户单击'plus'图像时获得该值,这意味着扩展了网格视图,那么我可以使用此Id更新相应行的一些字段。是否有其他方法来获取Id而不使用SelectedIndexChanged事件
我用下面的代码片段尝试了一下,效果很好:
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a href="JavaScript:divexpandcollapse('div<%# Eval("ClaimMasterId") %>');">
<img id="imgdiv<%# Eval("ClaimMasterId") %>" width="9" border="0" src="Img1/plus.gif" alt="" title="Add Action Notes" />Link</a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<script type="text/javascript">
function divexpandcollapse(divname) {
document.getElementById('<%= TextBox1.ClientID %>').value = divname;
}
</script>
经过一段艰难的时间后,我撤回了javascript概念,并使用Datakeys使用SelectedIndexChanged事件。
添加到Gridview
DataKeyNames="ClaimMasterId"
在c#中
ClaimMasterIdHF.Value = (gvClaimMaster.DataKeys[gvClaimMaster.SelectedIndex].Values["ClaimMasterId"]).ToString();