首页 新闻 会员 周边 捐助

GridView多行编辑,取不到值!

0
悬赏园豆:100 [已关闭问题]
<P>&nbsp;&nbsp;&nbsp; 有个需求就是同时支持GRIDVIEW里面的所有行同时编辑,最后在页面下方点击“保存”来遍历保存GRIDVIEW上的所有项。</P> <P>&nbsp;&nbsp;&nbsp; 为了使GRIDVIEW能够同时支持编辑,我在GridView1_RowDataBound 事件里写入了以下处理:</P><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> <DIV class=cnblogs_code><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">protected</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;GridView1_RowDataBound(</SPAN><SPAN style="COLOR: #0000ff">object</SPAN><SPAN style="COLOR: #000000">&nbsp;sender,&nbsp;GridViewRowEventArgs&nbsp;e)<BR><IMG id=Codehighlighter1_85_590_Open_Image onclick="this.style.display='none'; document.getElementById('Codehighlighter1_85_590_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_85_590_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_85_590_Closed_Text').style.display='inline';" src="http://space.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_85_590_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_85_590_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_85_590_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_85_590_Open_Text').style.display='inline';" src="http://space.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN id=Codehighlighter1_85_590_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://space.cnblogs.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_85_590_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;count&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;e.Row.Cells.Count;<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;((e.Row.RowType&nbsp;</SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000">&nbsp;DataControlRowType.Header)&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;&amp;</SPAN><SPAN style="COLOR: #000000">&nbsp;(e.Row.RowType&nbsp;</SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000">&nbsp;DataControlRowType.Footer))<BR><IMG id=Codehighlighter1_240_577_Open_Image onclick="this.style.display='none'; document.getElementById('Codehighlighter1_240_577_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_240_577_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_240_577_Closed_Text').style.display='inline';" src="http://space.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_240_577_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_240_577_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_240_577_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_240_577_Open_Text').style.display='inline';" src="http://space.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN id=Codehighlighter1_240_577_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://space.cnblogs.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_240_577_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">for</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #800080">2</SPAN><SPAN style="COLOR: #000000">;&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;count;&nbsp;i</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">)<BR><IMG id=Codehighlighter1_298_567_Open_Image onclick="this.style.display='none'; document.getElementById('Codehighlighter1_298_567_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_298_567_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_298_567_Closed_Text').style.display='inline';" src="http://space.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_298_567_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_298_567_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_298_567_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_298_567_Open_Text').style.display='inline';" src="http://space.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN id=Codehighlighter1_298_567_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://space.cnblogs.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_298_567_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TextBox&nbsp;tb&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;TextBox();<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tb.ID&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">txt</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;e.Row.RowIndex.ToString();<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tb.Text&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;e.Row.Cells[i].Text&nbsp;</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">&amp;nbsp;</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">?</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #800000">""</SPAN><SPAN style="COLOR: #000000">&nbsp;:&nbsp;e.Row.Cells[i].Text;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.Row.Cells[i].Controls.Add(tb);<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN></DIV> <DIV class=cnblogs_code><SPAN><SPAN style="COLOR: #000000">做完之后产生如下效果:</SPAN></SPAN></DIV><SPAN><SPAN style="COLOR: #000000"> <DIV class=cnblogs_code> <TABLE id=GridView1 style="BORDER-COLLAPSE: collapse" cellSpacing=0 rules=all border=1> <TBODY> <TR> <TH scope=col>学号</TH> <TH scope=col>姓名</TH> <TH scope=col>隐藏学号</TH> <TH scope=col>法制规范</TH> <TH scope=col>学习表现</TH> <TH scope=col>宿舍表现</TH></TR> <TR> <TD>stu001</TD> <TD>张三</TD> <TD>stu001</TD> <TD><INPUT id=GridView1_ctl02_txt0 value=4.0 name=GridView1$ctl02$txt0></TD> <TD><INPUT id=GridView1_ctl02_txt0 value=4.0 name=GridView1$ctl02$txt0></TD> <TD><INPUT id=GridView1_ctl02_txt0 value=4.0 name=GridView1$ctl02$txt0></TD></TR> <TR> <TD>stu002</TD> <TD>李四</TD> <TD>&nbsp;</TD> <TD><INPUT id=GridView1_ctl03_txt1 name=GridView1$ctl03$txt1></TD> <TD><INPUT id=GridView1_ctl03_txt1 name=GridView1$ctl03$txt1></TD> <TD><INPUT id=GridView1_ctl03_txt1 name=GridView1$ctl03$txt1></TD></TR></TBODY></TABLE></DIV> <DIV class=cnblogs_code>&nbsp; &nbsp; &nbsp; &nbsp;<INPUT id=btnSave style="WIDTH: 87px" onclick=a(); type=submit value=保 存 name=btnSave></DIV> <DIV class=cnblogs_code>&nbsp;</DIV> <DIV class=cnblogs_code>当我点击“保存”时就出问题了,我这样遍历:</DIV> <DIV class=cnblogs_code>&nbsp;<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> <DIV class=cnblogs_code><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">foreach</SPAN><SPAN style="COLOR: #000000">&nbsp;(GridViewRow&nbsp;gvr&nbsp;</SPAN><SPAN style="COLOR: #0000ff">in</SPAN><SPAN style="COLOR: #000000">&nbsp;GridView1.Rows)<BR><IMG id=Codehighlighter1_56_113_Open_Image onclick="this.style.display='none'; document.getElementById('Codehighlighter1_56_113_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_56_113_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_56_113_Closed_Text').style.display='inline';" src="http://space.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_56_113_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_56_113_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_56_113_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_56_113_Open_Text').style.display='inline';" src="http://space.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN id=Codehighlighter1_56_113_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://space.cnblogs.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_56_113_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&nbsp;value&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;gvr.Cells[</SPAN><SPAN style="COLOR: #800080">3</SPAN><SPAN style="COLOR: #000000">].Text;</SPAN></SPAN></DIV> <DIV class=cnblogs_code><SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN></DIV> <DIV class=cnblogs_code><SPAN><SPAN style="COLOR: #000000">&nbsp;这样可以取到值,但是到不到我 <FONT color=#ff0000>修改之后的值</FONT>。比如我把一个框里的4.0修改了,但是取出来的还是4.0</SPAN></SPAN></DIV></SPAN></SPAN></DIV> <DIV class=cnblogs_code><SPAN><SPAN style="COLOR: #000000">或是&nbsp; gvr.FindControl("id")这样也不行。</SPAN></SPAN></DIV> <DIV class=cnblogs_code><SPAN><SPAN style="COLOR: #000000">因为本GRIDVIEW里面的所有列均为不定项,因此用<FONT color=#ff0000>全模板列</FONT>也不太方便实现。</SPAN></SPAN></DIV> <DIV class=cnblogs_code><SPAN><SPAN style="COLOR: #000000">希望大家火速指教!!</SPAN></SPAN></DIV>
水丰寸的主页 水丰寸 | 初学一级 | 园豆:100
提问于:2008-08-12 14:09
< >
分享
其他回答(1)
0
tb.Text = e.Row.Cells[i].Text == "&nbsp;" ? "" : e.Row.Cells[i].Text; 这样好像无论如何取道的值都是不会变的吧 http://files.cnblogs.com/clhed/downComicWeb.rar 这儿有个例子 (例子更新过了) 换个数据源就可以用 这里有个全选可以改变gridview里的选定项目
小眼睛老鼠 | 园豆:2731 (老鸟四级) | 2008-08-12 15:18
0
我想问楼主想要实现什么功能,如果想用TextBox框,我觉得可以转为模板列,
Annie | 园豆:396 (菜鸟二级) | 2008-08-13 09:07
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册