当前位置:编程学习 > C#/ASP.NET >>

请教达人:gridview如何插入不可编辑数据

现在的做法是用文本框: 
<InsertItemTemplate>
                              <asp:TextBox runat="server"
                                                     ID="TextBoxN" 
                                                    Text='<%# Bind("username") %>'
                                                     Value='<%# getUsername()  %>'>
                             </asp:TextBox> 
 </InsertItemTemplate>
这样一来文本框能被修改.
但是如果 文本框设为 Visible="False" 或Enabled="False" 都取不到数据,请问如何解决?
不胜感激! --------------------编程问答--------------------
引用楼主 placido07 的回复:
现在的做法是用文本框: 
<InsertItemTemplate>
  <asp:TextBox runat="server"
  ID="TextBoxN" 
  Text='<%# Bind("username") %>'
  Value='<%# getUsername() %>'>
  </asp:TextBox> 
 </InsertItemTemplate>
这样一来文……

表示不明白LZ你的意思,如果你要插入的数据不可编辑直接Enable=false即可不知道你具体要什么效果~ --------------------编程问答-------------------- (gridview.Rows[索引].FindControl("TextBoxN") as TextBox).Text; --------------------编程问答--------------------

<!--直接设置enabled=false-->
 <ItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Enabled=false Text='<%#Eval("Product") %>'></asp:TextBox>
                </ItemTemplate>


//获取要看你在什么地方获取,这里是gridview外的button获取
    protected void Button5_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView4.Rows)
        {
            TextBox txt = row.FindControl("TextBox3") as TextBox;
            Response.Write(txt.Text + "<br/>");
        }
    }
--------------------编程问答--------------------
<asp:TemplateField>
                <ItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Enabled=false Text='<%#Eval("Product") %>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
            <ItemTemplate>
            <asp:Button ID="Button5" runat="server" Text="Button" onclick="Button5_Click" />
            </ItemTemplate>
            </asp:TemplateField>


//如果gridview里面的button获取的话
 protected void Button5_Click(object sender, EventArgs e)
    {
        int index = (((sender as Button).NamingContainer) as GridViewRow).RowIndex;
        TextBox txt = GridView4.Rows[index].FindControl("TextBox3") as TextBox;
        Response.Write(txt.Text);
    }
--------------------编程问答-------------------- 把文本框去掉直接写<%#Eval("username") %> --------------------编程问答-------------------- 或者换成lable --------------------编程问答--------------------
<asp:TemplateField>
                <ItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Enabled=false Text='<%#Eval("Product") %>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
            <ItemTemplate>
            <asp:Button ID="Button5" runat="server" Text="Button" CommandName="getValue" />
            </ItemTemplate>
            </asp:TemplateField>


//或设置commandname,获取方法和上面差不多
 protected void GridView4_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "getValue")
        {
            int index = ((e.CommandSource as Button).NamingContainer as GridViewRow).RowIndex;
            TextBox txt = GridView4.Rows[index].FindControl("TextBox3") as TextBox;
            Response.Write(txt.Text);
        }
    }
--------------------编程问答-------------------- 要是永远不可编辑的话,就用label呗。。。 --------------------编程问答-------------------- label --------------------编程问答-------------------- 建议直接用Label,负责事情简单化 --------------------编程问答-------------------- 不想被编辑,就直接用label标签啊, --------------------编程问答-------------------- 设置文本框的ReadOnly属性为true即可 --------------------编程问答-------------------- Enable=false就可以了 --------------------编程问答-------------------- 谢谢诸位的解答:
我再说下我的需求吧:
我其实是用Formview(更正一下),我要从当前环境中获取用户名,同时要把它写入publisher字段.

<asp:FormView ID="FormView1" runat="server" DataKeyNames="ID" DataSourceID="SqlDataSource1"
        DefaultMode="Insert" Width="100%">
        <EditItemTemplate>
            
            此处省略....
        </EditItemTemplate>
        <InsertItemTemplate>
             ....
             <asp:TextBox ID="TextBoxPublisher" runat="server" Text='<%# Bind("publisher") %>'
                Value='<%# getUsername()  %>' Enabled="False" />
        </InsertItemTemplate>
        <ItemTemplate>
            此处省略....
        </ItemTemplate>
    </asp:FormView> 

1.使用Enabled="False"的后果是:页面可以显示也不可编辑,但是写入不了数据库.
2.使用asp:Label直接替换asp:TextBox的后果是,页面不能显示,也写入不了数据库
各位有何高见能解决这个需求?谢谢 --------------------编程问答-------------------- 这真的是个问题如我在14#楼写的, --------------------编程问答-------------------- 没看明白~ --------------------编程问答-------------------- 不能修改就用label啊~ --------------------编程问答--------------------
引用 17 楼 wx8849 的回复:
不能修改就用label啊~

用label 不能初始化值啊,没有label 貌似没有Value这个属性 --------------------编程问答-------------------- 直接label不就结了。。。。。 --------------------编程问答-------------------- label.Text
.................. --------------------编程问答--------------------
引用 19 楼 starfd 的回复:
直接label不就结了。。。。。

1.使用Enabled="False"的后果是:页面可以显示也不可编辑,但是写入不了数据库.
2.使用asp:Label直接替换asp:TextBox的后果是,页面不能显示,也写入不了数据库

困扰已久的问题了! --------------------编程问答--------------------
引用 11 楼 jolyloving 的回复:
不想被编辑,就直接用label标签啊,

但是还要把这个数据初始化同时要绑定到sqldatasource的一个字段上,如何实现呢? --------------------编程问答-------------------- 请放弃使用gridview吧,有很多其他方式的列表
例如:手动拼代码,flexgrid --------------------编程问答-------------------- 没看明白 --------------------编程问答--------------------
引用 23 楼 wcj1018_net 的回复:
请放弃使用gridview吧,有很多其他方式的列表
例如:手动拼代码,flexgrid

确实头疼
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,