Gridview Row -FindControl()不返回值 [英] Gridview Row -FindControl() not returning value

查看:73
本文介绍了Gridview Row -FindControl()不返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用sqlserver,三层架构和存储过程...



我的aspx页面




I am using sqlserver, three tier architecture and store procedure for this...

My aspx Page


<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

	            onrowcancelingedit="GridView1_RowCancelingEdit"

	            onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"

	            onrowupdating="GridView1_RowUpdating">
	        <columns>
	        <asp:TemplateField HeaderText="ProductId">
	        <itemtemplate>
	        <asp:Label ID="lblProductId" runat="server" Text='<%#Eval("ProductId") %>'>'> 
	        </itemtemplate>
	        
	         <asp:TemplateField HeaderText="ProductCategory">
	        <itemtemplate>
	        <asp:Label ID="lblProductCategory" runat="server" Text='<%#Eval("ProductCategory") %>'>'> 
	        </itemtemplate>
	        <edititemtemplate>
	        <asp:TextBox ID="txtProductCategory" runat="server" Text='<%#Eval("ProductCategory") %>'>' >
	        </edititemtemplate>
            
	         <asp:TemplateField HeaderText="GirthFrom">
	        <itemtemplate>
	        <asp:Label ID="lblGirthFrom" runat="server" Text='<%#Eval("GirthFrom") %>'>'> 
	        </itemtemplate>
	        <edititemtemplate>
	        <asp:TextBox ID="txtGirthFrom" runat="server" Text='<%#Eval("GirthFrom") %>'>' >
	        </edititemtemplate>
	        
             <asp:TemplateField HeaderText="GirthTo">
	        <itemtemplate>
	        <asp:Label ID="lblGirthTo" runat="server" Text='<%#Eval("GirthTo") %>'>'> 
	        </itemtemplate>
	        <edititemtemplate>
	        <asp:TextBox ID="txtGirthTo" runat="server" Text='<%#Eval("GirthTo") %>'>' >
	        </edititemtemplate>
	        
	            <asp:TemplateField HeaderText="Action">
                    <itemtemplate>
                        <asp:LinkButton ID="edit" runat="server" CommandName="Edit" Text="Edit">
                        <asp:LinkButton ID="Delete" runat="server" CommandName="Delete" Text="Delete">
                    </itemtemplate>
                    <edititemtemplate>
                        <asp:LinkButton ID="Update" runat="server" CommandName="Update" Text="Update">
                        <asp:LinkButton ID="Cancel" runat="server" CommandName="Cancel" Text="cancel">
                    </edititemtemplate>
                
	        </columns>





我的aspx.cs页面





My aspx.cs Page

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
 
public partial class _Default : System.Web.UI.Page
{
    Creation obj=new Creation();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
 
            Gridviewdatabind();
        }
            
        
    }
    private void Gridviewdatabind()
    {
        DataTable dt = obj.display1();
            GridView1.DataSource = dt;
            GridView1.DataBind();
    }
 
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string lblProductId = ((Label)GridView1.Rows[e.RowIndex].FindControl("lblProductId")).Text;
        obj.ProductId = Convert.ToInt32(lblProductId);
        int res = obj.delete();
        Gridviewdatabind();
        
        
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        Gridviewdatabind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string lblProductId = ((Label)GridView1.Rows[e.RowIndex].FindControl("lblProductId")).Text;
 
        string txtProductCategory = ((TextBox)GridView1.Rows[e.RowIndex]
                           .FindControl("txtProductCategory")).Text;
        string txtGirthFrom = ((TextBox)GridView1.Rows[e.RowIndex]
                           .FindControl("txtGirthFrom")).Text;
        string txtGirthTo = ((TextBox)GridView1.Rows[e.RowIndex]
                               .FindControl("txtGirthTo")).Text;
        obj.ProductCategory = txtProductCategory;
        obj.GirthFrom = Convert.ToInt32(txtGirthFrom);
        obj.GirthTo = Convert.ToInt32(txtGirthTo);
        obj.ProductId = Convert.ToInt32(lblProductId);
        int res = obj.update();
        GridView1.EditIndex = -1;
        Gridviewdatabind();
 
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        Gridviewdatabind();
    }
    
}





我的Datalayer(databaseoperatio类文件)





My Datalayer(databaseoperatio class file)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for DBoperations
/// </summary>
public class DBoperations
{
    SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Micro;Integrated Security=True");
    SqlCommand cmd = null;
	public DBoperations()
	{
		//
		// TODO: Add constructor logic here
		//
	}

public int UPDATECATEGORY(Categorycreation obj)
    {
        cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "SP_Updatecategory";
        cmd.Parameters.AddWithValue("@pdtctg", obj.ProductCategory);
        cmd.Parameters.AddWithValue("@girfrom", obj.GirthFrom);
        cmd.Parameters.AddWithValue("@girto", obj.GirthTo);
        cmd.Parameters.AddWithValue("@pdtid", obj.ProductId);
        con.Open();
        int res = cmd.ExecuteNonQuery();
        con.Close();
        return res;
    }
}









< b>我的属性层(类文件)







My property layer(class file)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// Summary description for Categorycreation
/// </summary>
public class Categorycreation
{
    DBoperations dbo = new DBoperations();
    public int _Pdtid;
    public string _Pdtctg;
    public int _girfrom;
    public int _girto;
    public int ProductId
    {
        get
        {
            return _Pdtid;
        }
        set
        {
            _Pdtid = value;
        }
    }
    public string ProductCategory
    {
        get
        {
            return _Pdtctg;
        }
        set
        {
            _Pdtctg = value;
        }
    }
    public int GirthFrom
    {
        get
        {
            return _girfrom;
        }
        set
        {
            _girfrom = value;
        }
    }
    public int GirthTo
    {
        get
        {
            return _girto;
        }
        set
        {
            _girto = value;
        }
    }
    public Categorycreation()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public int update()
    {
        int res = dbo.UPDATECATEGORY(this);
        return res;
    }
}





我存储的程序





My stored Procedure

ALTER PROCEDURE SP_Updatecategory(@pdtctg nvarchar(50),@girfrom int,@girto int,@pdtid int)

AS
BEGIN
update Category set ProductCategory=@pdtctg,GirthFrom=@girfrom,GirthTo=@girto where ProductId=@pdtid
    END



问题是单击更新链接按钮时,gridview文本框中的文本无法读取..... aspx.cs pag以下代码不工作


The Problem is While clicking the update linkbutton the text from the textbox of the gridview is no reading..... that is in aspx.cs pag the following code is not workinge

string txtProductCategory = ((TextBox)GridView1.Rows[e.RowIndex]
                           .FindControl("txtProductCategory")).Text;
        string txtGirthFrom = ((TextBox)GridView1.Rows[e.RowIndex]
                           .FindControl("txtGirthFrom")).Text;
        string txtGirthTo = ((TextBox)GridView1.Rows[e.RowIndex]
                               .FindControl("txtGirthTo")).Text;







请帮帮我...




please help me...

推荐答案

GridViewRow row =(GridViewRow)GridView1.Rows [e.RowIndex];

Label lblProductId =(Label)row.FindControl(lblProductId);

obj.ProductId = Convert.ToInt32(lblProductId);

int res = obj。删除();

Gridviewdatabind();





试试这个。
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lblProductId = (Label)row.FindControl("lblProductId");
obj.ProductId = Convert.ToInt32(lblProductId);
int res = obj.delete();
Gridviewdatabind();


try this.


查找控件的一种方法是在事件中命名容器..可能有帮助..



GridViewRow row =((linkbutton)sender).NamingContainer作为GridViewRow;

然后使用

找到控件TextBox name = row.FindControl( - yourHTMLID--)作为TextB牛;



然后是name.Text等等......可以这个帮助
one way to find the control is naming container in the event.. may this help..

GridViewRow row = ((linkbutton)sender ).NamingContainer as GridViewRow;
and then Find the Controls using
TextBox name = row.FindControl("--yourHTMLID--") as TextBox ;

and then name.Text and so on... may this Help


这篇关于Gridview Row -FindControl()不返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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