想修改DataGrid中TextBox的值,可是修改不了,老是读取原来的值.请各位高手帮忙修改一下吧!<asp:TemplateColumn HeaderText="Top10">
<ItemStyle HorizontalAlign="Center" Width="10%"></ItemStyle>
<ItemTemplate>
<asp:textbox id="txtTop10" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Top10") %>' Width="20px">
</asp:textbox>
<asp:LinkButton runat="server" Text="修改" CommandName="Top10" CausesValidation="false" ID="lbtnTop10"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>代码:
if ( e.CommandName == "Top10" ) 
{
int productid = int.Parse(e.Item.Cells[0].Text.ToString());
int top10=int.Parse(((TextBox)this.HeadlinesGrid.Items[e.Item.ItemIndex].Cells[9].FindControl("txtTop10")).Text.ToString());
// int top10=5;
try
{
myiproduct.UpdateTop10(productid,top10);
HeadlinesGrid.CurrentPageIndex=0;
Bindcouter();
BindGrid();
}

解决方案 »

  1.   

    應該改邦給DataGrid的數據源,而不是改DataGrid的吧
    ,DataGrid只是個表示的東東而已,你改後又沒去改動數據源
      

  2.   

    我是想改数据源,可是读不到我改后的textbox的值
      

  3.   

    先確定你在取值之前,是否重新bind過
      

  4.   

    你bind是不是写在pageload里面 加了!IsPostBack判断没
      

  5.   

    发现个问题,我的pageload里并没有加bind,可是为什么会自动绑定datagrid呢?
      

  6.   

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    _ID= (Request.Params["ID"] ==null)?-1:( int.Parse(Request.Params["ID"]));
    _SearchText= (Request.Params["SearchText"] ==null)?"":( Request.Params["SearchText"]); if (!IsPostBack)
    {
    TxtSearch.Text=_SearchText; CreateDocTree();  DropDownList1.Items.Insert(0,new ListItem("所有列表","-1"));

    DropDownList1.SelectedIndex=DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(_ID.ToString()));
    }
    } /// <summary>
    /// 生成树形下拉菜单
    /// </summary>
    private void  CreateDocTree()
    {
    DataTable dtblCategory = new DataTable( "Category" );
    // Create Columns
    DataColumn dcolColumn = new DataColumn( "CategoryID", typeof( Int32 ) );
    dtblCategory.Columns.Add( dcolColumn ); 
    dcolColumn = new DataColumn( "FatherID", typeof( Int32  ) );
    dtblCategory.Columns.Add( dcolColumn );
    dcolColumn = new DataColumn( "Name", typeof( string  ) );
    dtblCategory.Columns.Add( dcolColumn );

    ICategory myicategory=new ICategory();
    DataTable dv = myicategory.ProductCategoryGetlist(0);
    int nodecounts=dv.Rows.Count;
    for(int i=0;i<nodecounts;i++)
    {
    int  id=Convert.ToInt32(dv.Rows[i][0].ToString());
    int hifid=Convert.ToInt32(dv.Rows[i][1].ToString());
    string foldername=dv.Rows[i][2].ToString(); DataRow drowItem = dtblCategory.NewRow();
    drowItem[ "CategoryID" ] = id;
    drowItem[ "FatherID" ] = hifid;
    drowItem[ "Name" ] = foldername;
    dtblCategory.Rows.Add( drowItem ); LoadSubFolder(ref dtblCategory,id,1); DropDownList1.DataSource = dtblCategory.DefaultView;
    DropDownList1.DataTextField="Name";
    DropDownList1.DataValueField="CategoryID";
    DropDownList1.DataBind();
    }
    }
    private void  LoadSubFolder(ref DataTable dtblCategory , int hifolderid,int deep)
    {
    ICategory myicategory=new ICategory();
    DataTable dv =  myicategory.ProductCategoryGetlist(hifolderid);
    int nodecounts=dv.Rows.Count;
    if(nodecounts!=0)
    {
    for(int i=0;i<nodecounts;i++)
    {
    int  id=Convert.ToInt32(dv.Rows[i][0].ToString());
    int hifid=Convert.ToInt32(dv.Rows[i][1].ToString());
    string foldername=dv.Rows[i][2].ToString(); DataRow drowItem = dtblCategory.NewRow();
    drowItem[ "CategoryID" ] = id;
    drowItem[ "FatherID" ] = hifid; string strdeep=""; for(int j=0;j<deep;j++)
    {
    strdeep = strdeep + " ";
    }
    drowItem[ "Name" ] = strdeep+foldername;
    dtblCategory.Rows.Add( drowItem );
    LoadSubFolder(ref dtblCategory,id,deep+1);
    }
    }
    }
    protected void Bindcouter() 
    {
    IDAL.IProduct myiproduct=new IProduct();
    int recordCount = myiproduct.GetList(_ID,_SearchText,true);
    pager1.RecordCount=recordCount;

    }
    protected void BindGrid() 
    {
    IDAL.IProduct myiproduct=new IProduct();
    DataTable mytable=new DataTable();
    mytable = myiproduct.GetList(_ID,_SearchText,true,pager1.PageSize,pager1.CurrentPageIndex);
    HeadlinesGrid.DataSource = mytable;
    HeadlinesGrid.DataBind();
    pager1.CustomInfoText="记录总数:<font color=\"blue\"><b>"+pager1.RecordCount.ToString()+"</b></font>";
    pager1.CustomInfoText+=" 总页数:<font color=\"blue\"><b>"+pager1.PageCount.ToString()+"</b></font>";
    pager1.CustomInfoText+=" 当前页:<font color=\"red\"><b>"+pager1.CurrentPageIndex.ToString()+"</b></font>";
    }private void pager1_PageChanged(object src, Wuqi.Webdiyer.PageChangedEventArgs e)
    {
    Bindcouter();
    pager1.CurrentPageIndex=e.NewPageIndex;
    BindGrid();
    } private void HeadlinesGrid_ItemCommand_1(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    IProduct myiproduct=new IProduct();
    if ( e.CommandName == "Delete" ) 
    {
    int productid = int.Parse(e.Item.Cells[0].Text.ToString());
    try
    {
    myiproduct.ProductDelete(productid);
    this.lblMsg.Text="删除成功!";
    HeadlinesGrid.CurrentPageIndex=0;
    Bindcouter();
    BindGrid();
    }
    catch
    {
    this.lblMsg.Text="删除失败!";
    }

    else if ( e.CommandName == "Hot" ) 
    {
    int productid = int.Parse(e.Item.Cells[0].Text.ToString());
    try
    {
    myiproduct.UpdateHot(productid);
    HeadlinesGrid.CurrentPageIndex=0;
    Bindcouter();
    BindGrid();
    }
    catch
    {
    this.lblMsg.Text="失败!";
    }
    } else if ( e.CommandName == "Spe" ) 
    {
    int productid = int.Parse(e.Item.Cells[0].Text.ToString());
    try
    {
    myiproduct.UpdateSpe(productid);
    HeadlinesGrid.CurrentPageIndex=0;
    Bindcouter();
    BindGrid();
    }
    catch
    {
    this.lblMsg.Text="失败!";
    }
    }
    else if ( e.CommandName == "New" ) 
    {
    int productid = int.Parse(e.Item.Cells[0].Text.ToString());
    try
    {
    myiproduct.UpdateNew(productid);
    HeadlinesGrid.CurrentPageIndex=0;
    Bindcouter();
    BindGrid();
    }
    catch
    {
    this.lblMsg.Text="失败!";
    }
    } else if ( e.CommandName == "Lack" ) 
    {
    int productid = int.Parse(e.Item.Cells[0].Text.ToString());
    try
    {
    myiproduct.UpdateLack(productid);
    HeadlinesGrid.CurrentPageIndex=0;
    Bindcouter();
    BindGrid();
    }
    catch
    {
    this.lblMsg.Text="失败!";
    }
    }
    else if ( e.CommandName == "Top10" ) 
    {
    int productid = int.Parse(e.Item.Cells[0].Text.ToString());
    int top10=int.Parse(((TextBox)this.HeadlinesGrid.Items[e.Item.ItemIndex].Cells[9].FindControl("txtTop10")).Text.ToString());
    // int top10=5;
    try
    {
    myiproduct.UpdateTop10(productid,top10);
    HeadlinesGrid.CurrentPageIndex=0;
    Bindcouter();
    BindGrid();
    }
    catch
    {
    this.lblMsg.Text="失败!";
    }
    }
    }
      

  7.   

    BindGrid() 到底在哪里 调用了啊 没看到 大哥
      

  8.   

    就是初始化展现的BindGrid() 到底在哪里 调用
      

  9.   

    奇怪的是,虽然没有调用,但是运行时,它就自动运行BindGrid()
      

  10.   

    private void pager1_PageChanged(object src, Wuqi.Webdiyer.PageChangedEventArgs e)
    {
    Bindcouter();
    pager1.CurrentPageIndex=e.NewPageIndex;
    BindGrid();
    }这个是干什么滴
      

  11.   

    pager1_PageChanged 估计和这个有关
      

  12.   

    那个值是可以改的,我直接给他一个值的话,就改了,但是读textbox的值的话就不对,原因可能是因为在Page_Load,BindGrid()虽然没有调用,但是运行时,它就自动运行BindGrid()
      

  13.   

    和databind()关系不大,我以前写过,好像和一个自动返回的属性有关系,记不清叫什么了,你找下sdk。
      

  14.   

    如果是题目那样问的话,要获取值,FINDCONTROL不就可以了吗
      

  15.   

    <asp:datagrid id="GridAdmin" runat="server" Width="279px" DataKeyField="s_id" AutoGenerateColumns="False"
    ShowFooter="false" AllowPaging="True" PageSize="15" BorderColor="#B8B4A3" BorderWidth="1px">//要把DataKeyField用主键<asp:TemplateColumn HeaderText="街道名称">
    <ItemStyle HorizontalAlign="left"></ItemStyle>
    <ItemTemplate>
    <asp:Label id=NameLabel runat="server" Width="100" Text='<%# DataBinder.Eval(Container.DataItem, "s_name") %>'>
    </asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox id=edit_Name runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "s_name") %>'>
    </asp:TextBox>
    <asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" Display="Dynamic" ControlToValidate="edit_Name">不可以空白</asp:RequiredFieldValidator>
    </EditItemTemplate>
    </asp:TemplateColumn><asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" HeaderText="编辑" CancelText="取消" EditText="编辑">
    <ItemStyle Wrap="False" Width="70px"></ItemStyle>
    </asp:EditCommandColumn>
      

  16.   

    后台:
    if (!Page.IsPostBack)
    {
    BindGrid();
                  }
    void BindGrid()
    {
    //绑定 你的DataGrid
    }
    private void InitializeComponent()
    {    
    this.GridAdmin.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.GridAdmin_EditCommand);
    this.GridAdmin.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.GridAdmin_UpdateCommand);

    this.Load += new System.EventHandler(this.Page_Load); }
    private void GridAdmin_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    int id = Convert.ToInt32(GridAdmin.DataKeys[e.Item.ItemIndex].ToString());
    yksl.BLL.street street = new yksl.BLL.street();
    TextBox CurrentTextBox;
    CurrentTextBox = (TextBox)e.Item.FindControl("edit_Name");
    you_update(.........
    //写个方法更新数据库
    BindGrid();//重新绑 
    }