gridview空单元格的赋值与取值

czhhlove 2011-07-26 09:19:51
gridview有一列其中单元格式空的 列代码:<asp:BoundField HeaderText="列名称" DataField="fieldName" HeaderStyle-Width="15%" /> js赋值的时候是document.getElementById("gvwExcelField").rows(3).cells(1).innerHTML="C_Name" 然后在Gridview中这个值也显示出来了 但是在后台取的时候 string xx = gvwExcelField.Rows[3].Cells[1].Text; xx总是等于" " 求解!
...全文
387 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
zqxinzqx 2011-10-09
  • 打赏
  • 举报
回复
大哥,我照着你的做但是label弹出来的还是label,而hiddenfield弹出来是空,到底是什么原因
czhhlove 2011-07-29
  • 打赏
  • 举报
回复
Gridview的样式 在模板列里放Label和HiddenFiled

<asp:GridView ID="gvwExcelField" runat="server" Width="100%" AutoGenerateColumns="False"
OnRowCommand="gvwExcelField_RowCommand">
<Columns>
<asp:BoundField HeaderText="序号" DataField="columnSortID" HeaderStyle-Width="6%" HeaderStyle-Height="35" />
<asp:TemplateField HeaderText="列名称" HeaderStyle-Width="15%">
<ItemTemplate>
<asp:Label ID="lblFiledName" runat="server" Text='<%# Eval("FieldName") %>' name="lblFiledName"></asp:Label>
<asp:HiddenField ID="HidFiledName" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="对应操作" HeaderStyle-Width="15%">
<ItemTemplate>
<asp:ImageButton ID="IbtRowUp" runat="server" ImageUrl="~/Import/Images/uparrow.gif"
Style="cursor: pointer" CommandName="row_up" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"columnSortID")%>' />
<asp:ImageButton ID="IbtRowDown" runat="server" ImageUrl="~/Import/Images/downarrow.gif"
Style="cursor: pointer" CommandName="row_down" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"columnSortID")%>' />
<img src="Images/field.gif" style="cursor: pointer" alt="修改字段" onclick="ModifyExcelFiled('<%#Eval("columnSortID") %>')" />
<img src="Images/del_None.gif" style="cursor: pointer" alt="清除字段" onclick="DeleteExcelFiled('<%#Eval("columnSortID") %>')" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

下面是给单元格赋值 在模板列里放置一个Label和一个HiddenFiled 给空单元格赋值的时候同时值赋给这两个空件

if (win) {
var obj = document.getElementById("gvwExcelField");
obj.rows(rowCount - 1 + 1).cells[1].childNodes[0].innerText = win;
obj.rows(rowCount - 1 + 1).cells[1].childNodes[1].value = win;
}


取值的时候 先取Label的值 如果Label的值为空(label取到的值一定为空) 就取HiddenFiled的值

string ExcelFiled = ((Label)gvwExcelField.Rows[b].FindControl("lblFiledName")).Text;
if (ExcelFiled == "")
{
ExcelFiled = ((HiddenField)gvwExcelField.Rows[b].FindControl("HidFiledName")).Value;
}
if (ExcelFiled != "")
{
//此处代码省略
}
zhou_xuexi 2011-07-26
  • 打赏
  • 举报
回复
你可以把名字取得不一样啊
czhhlove 2011-07-26
  • 打赏
  • 举报
回复
现在是一个模板列啊 这一列的每个单元格都有相同的name 要怎么取到每个单元格的值呢
zhou_xuexi 2011-07-26
  • 打赏
  • 举报
回复
服务器控件也是一样的
zhou_xuexi 2011-07-26
  • 打赏
  • 举报
回复
前台代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="jQuery-14.aspx.cs" Inherits="jQuery_14" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<style type="text/css">
#panel{position:relative;width:100px;height:100px;border:1px solid #0050D0;background:#96E555;cursor:pointer;}
</style>
<script type="text/javascript" src="jquery-1.6.1.min.js"></script>
<script type="text/javascript">
$(function(){
// $("#panel").toggle(function(){
// $(this).animate({left: "+=500px",height:"+=200px"},3000);
// },function(){
// $(this).animate({left: "-=500px",height:"-=200px"},3000);
// })
$("#panel").css("opacity","0.5");
$("#panel").click(function(){
$(this).animate({left:"400px",height:"200px",opacity:"1"},3000)
.animate({top:"200px",width:"200px"},3000)
.fadeOut("slow");
})
})
function addOption()
{
var slt=document.getElementById("sl");
slt.options.add(new Option("1","测试"));
}
</script>
</head>
<body onload="addOption()">
<form id="form1" runat="server">
<div>
<div id="panel"></div>
<select name="slT" id="sl">

</select>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
后台代码获取
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class jQuery_14 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('" + Request.Form["slT"] + "')", true);
}
}
这里就是获取select控件js赋值的
czhhlove 2011-07-26
  • 打赏
  • 举报
回复
高人们 帮帮忙啊
czhhlove 2011-07-26
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 zhou_xuexi 的回复:]
js中赋值,在后台直接用gvwExcelField.Rows[3].Cells[1].Text是获取不到的,要给你js设置的那个元素设置name属性,然后用Request.Form["name属性名称"]来获取
[/Quote]
能不能给个例子


<asp:GridView ID="gvwExcelField" runat="server" Width="100%" AutoGenerateColumns="False"
OnRowCommand="gvwExcelField_RowCommand">
<Columns>
<asp:BoundField HeaderText="序号" DataField="columnSortID" HeaderStyle-Width="6%" HeaderStyle-Height="35" />
<%--<asp:BoundField HeaderText="列名称" DataField="fieldName" HeaderStyle-Width="15%" />--%>
<asp:TemplateField HeaderText="列名称" HeaderStyle-Width="15%">
<ItemTemplate>
<asp:Label ID="lblFiledName" runat="server" Text='<%# Eval("FieldName") %>' name="lblFiledName"></asp:Label> <asp:HiddenField ID="hidFiled" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="对应操作" HeaderStyle-Width="15%">
<ItemTemplate>
<asp:ImageButton ID="IbtRowUp" runat="server" ImageUrl="~/Import/Images/uparrow.gif"
Style="cursor: pointer" CommandName="row_up" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"columnSortID")%>' />
<asp:ImageButton ID="IbtRowDown" runat="server" ImageUrl="~/Import/Images/downarrow.gif"
Style="cursor: pointer" CommandName="row_down" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"columnSortID")%>' />
<%-- <asp:ImageButton ID="IbtModifyRow" runat="server" ImageUrl="~/Import/Images/field.gif"
Style="cursor: pointer" OnClick="ModifyExcelFiled('<%#Eval("columnSortID") %>')" />--%>
<img src="Images/field.gif" style="cursor: pointer" alt="修改字段" onclick="ModifyExcelFiled('<%#Eval("columnSortID") %>')" />
<img src="Images/del_None.gif" style="cursor: pointer" alt="清除字段" onclick="DeleteExcelFiled('<%#Eval("columnSortID") %>')" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


后台的话怎么获取呢 怎么获取 列名称 这一列的 每个单元格的值呢
zhou_xuexi 2011-07-26
  • 打赏
  • 举报
回复
js中赋值,在后台直接用gvwExcelField.Rows[3].Cells[1].Text是获取不到的,要给你js设置的那个元素设置name属性,然后用Request.Form["name属性名称"]来获取
czhhlove 2011-07-26
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 weslonlovejall 的回复:]
document.getElementById("gvwExcelField").rows(3).cells(1).innerHTML="C_Name"
你显示出来的有没有值呢?
[/Quote]

在gridview中显示出来了
风之雨 2011-07-26
  • 打赏
  • 举报
回复
document.getElementById("gvwExcelField").rows(3).cells(1).innerHTML="C_Name"
你显示出来的有没有值呢?
子夜__ 2011-07-26
  • 打赏
  • 举报
回复
gvwExcelField.Rows[3].Cells[1].Controls[0]
czhhlove 2011-07-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bin_520_yan 的回复:]
gvwExcelField.Rows[3].Cells[1].Value//改成Value看看
[/Quote]
不行 会报错
DataBox-MDX 2011-07-26
  • 打赏
  • 举报
回复
gvwExcelField.Rows[3].Cells[1].Value//改成Value看看
THG8888 2011-07-26
  • 打赏
  • 举报
回复
Label lblcontactNos = this.gvwExcelField.Rows[index].FindControl("lblFiledName") as Label;

string lblcontactNos = lblcontactNos.Text.ToString(); 这个不可以吗
czhhlove 2011-07-26
  • 打赏
  • 举报
回复
已经研究出来了 先结贴 答案稍后公布 谢谢各位了
zhou_xuexi 2011-07-26
  • 打赏
  • 举报
回复
<asp:TemplateField HeaderText="列名称" HeaderStyle-Width="15%">
<ItemTemplate>
<asp:Label ID="lblFiledName" runat="server" Text='<%# Eval("FieldName") %>' name='<% Eval("columnSortID")%>'></asp:Label> <asp:HiddenField ID="hidFiled" runat="server" />
</ItemTemplate>
</asp:TemplateField>用columnSortID命名不行吗
liue_0612 2011-07-26
  • 打赏
  • 举报
回复
在gvwExcelField_RowCommand 事件中获取值
int index = int.Parse(e.CommandArgument.ToString());
Label lblcontactNos = this.gvwExcelField.Rows[index].FindControl("lblFiledName") as Label;

string lblcontactNos = lblcontactNos.Text.ToString();
czhhlove 2011-07-26
  • 打赏
  • 举报
回复
<asp:TemplateField HeaderText="列名称" HeaderStyle-Width="15%">
<ItemTemplate>
<asp:Label ID="lblFiledName" runat="server" Text='<%# Eval("FieldName") %>' [color=#FF0000]name="lblFiledName"></asp:Label>[/color] <asp:HiddenField ID="hidFiled" runat="server" />
</ItemTemplate>
</asp:TemplateField>
怎么取不一样啊

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧