gridview中的绑定文本框 [英] binding textbox in gridview

查看:92
本文介绍了gridview中的绑定文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#,asp.net 2.0,server 2005和visual studio 2005



任何人都可以帮助解决以下问题:



最初我加载gridview(包含1个文本框),其中包含一些值...





I am using C#,asp.net 2.0,server 2005 and visual studio 2005

Can anybody help with the following problem:

initially i load gridview (contains 1 textbox) with some values using...


in aspx
<asp:TextBox Text='<%#Eval("col1") %>'/>

in C#:
 
Con.Open();
             SqlDataAdapter dt = new SqlDataAdapter("select col1 from Table1", Con);
             DataSet ds = new DataSet();
             dt.Fill(ds);
             GridView1.DataSource = ds;
             GridView1.DataBind();





现在我点击一个按钮,当我点击按钮时,gridview应加载一些新值,我使用以下函数绑定gridview:





now i click one buttton and when i click button the gridview should be loaded with some new values and i use the following function to bind the gridview.:

<pre>


in C#:

Con.Open();
             SqlDataAdapter dt = new SqlDataAdapter("select col2 from Table1", Con);
             DataSet ds = new DataSet();
             dt.Fill(ds);
             GridView1.DataSource = ds;
             GridView1.DataBind();


</pre>





我应该在



what i should write a common in

Text='<% %>'

,以便gridview绑定功能指定值











请帮帮我。

先谢谢。

问候,

Karan

, so that the gridview is binded with function specified values





please help me.
Thanks in Advance.
Regards,
Karan

推荐答案

看看 there-CP Question-Answer [ ^ ]同样的问题回复了很好的解决方案。

你应该写成

Take a look there-CP Question-Answer[^] same question is replied with nice solutions.
You should write as
<asp:textbox id="YourControlID" runat="Server" xmlns:asp="#unknown">
          Text='<%# Bind("ColumnName") %>'  /></asp:textbox>


我有两张桌子

I have two tables
dt.Columns.Add("Col1");
dt.Rows.Add("Col11");
dt1.Columns.Add("Col2");
dt1.Rows.Add("Col12");



我第一次使用dt绑定GridView1,第二次使用具有不同ColumnName的dt1绑定。

我正在检查DataSource属性


I am binding GridView1 first time with dt and second time with dt1 which are having different ColumnName.
And I am checking the DataSource property

public void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        TextBox txt = (TextBox)e.Row.FindControl("txtCol");
        if (GridView1.DataSource == dt)
        {
            txt.Text = DataBinder.Eval(e.Row.DataItem, "Col1").ToString();
        }
        else if (GridView1.DataSource == dt1)
        {
            txt.Text = DataBinder.Eval(e.Row.DataItem, "Col2").ToString();
        }
    }
}


您好,

您可以使用公共列别名sql查询的名称,并使用该别名列名进行数据绑定。比如下面这个代码,

in * .aspx

Hi,
You can use a common column alias name for sql queries and use that alias column name for data bind. Like try this code below,
in *.aspx
<asp:TextBox Text='<%#Eval("mycolumn") %>'/>



in * .aspx.cs


in *.aspx.cs

///For 1st DataBind
SqlDataAdapter dt = new SqlDataAdapter("select col1 as mycolumn from Table1", Con);
///For 2nd DataBind
SqlDataAdapter dt = new SqlDataAdapter("select col2 as mycolumn from Table1", Con);



你可以看到两个查询都使用相同的列别名,因此切换GridView的数据源不会影响绑定。



希望这会有所帮助。


as you can see both queries use same column alias, so switching data source for GridView wont effect the binding.

Hope this will help.


这篇关于gridview中的绑定文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆