网格视图编辑项目模板 [英] Grid view Edit Item template

查看:61
本文介绍了网格视图编辑项目模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Net;
using System.Configuration;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls.Expressions;

public partial class Admin_view_record_demo : System.Web.UI.Page
{

    Class1 cls;
    

    protected void Page_Load(object sender, EventArgs e)
    {

        cls = new Class1();
        if (!Page.IsPostBack)
        {
            
            this.bind_grd1();
        }
    }



    protected void DownloadFile(object sender, EventArgs e)
    {
        string filePath = (sender as LinkButton).CommandArgument;
        Response.ContentType = ContentType;
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
        Response.WriteFile(filePath);
        Response.End();
    }



    protected void bind_grd1()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["constring"]);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "select_grid_demo";
        cmd.Connection = con;
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        grd1.DataSource = ds;
        grd1.DataBind();

    }
    protected void grd1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grd1.EditIndex = e.NewEditIndex;
        this.bind_grd1();
    }
    protected void grd1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string admn_cmmnt=(grd1.Rows[e.RowIndex].FindControl("t1") as TextBox).Text;
        string aprORrjct = (grd1.Rows[e.RowIndex].FindControl("drpAOR_1") as DropDownList).SelectedItem.Value;
        string j_ID = grd1.DataKeys[e.RowIndex].Value.ToString();
        string strConnString = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
        using (SqlConnection con1 = new SqlConnection(strConnString))
        {
            string query1 = "update Admin_Added_Details set Admin_Comment=@Admin_Comment1,ApprovedORrejected=@ApprovedORrejected1 where ID=@ID1";
            using (SqlCommand cmd2 = new SqlCommand(query1))
            {
                cmd2.Connection = con1;
                cmd2.Parameters.AddWithValue("@Admin_Comment1", admn_cmmnt);
                cmd2.Parameters.AddWithValue("@ApprovedORrejected1",aprORrjct);
                cmd2.Parameters.AddWithValue("@ID1",j_ID);
                con1.Open();
                cmd2.ExecuteNonQuery();
                con1.Close();
            }
        }

         
    }
    
    protected void grd1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grd1.EditIndex = -1;
        bind_grd1();
    }




    protected void grd1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow && grd1.EditIndex==e.Row.RowIndex)
        {
                DropDownList ddrrpp = (DropDownList)e.Row.FindControl("drpAOR_1");
                string query = "select ApprovedORrejected from approved_OR_rejected ";
                SqlCommand cmd2 = new SqlCommand(query);
                ddrrpp.DataSource = GetData(cmd2);
                ddrrpp.DataTextField = "ApprovedORrejected";
                ddrrpp.DataValueField = "ApprovedORrejected"; 
                ddrrpp.DataBind();
                ddrrpp.Items.FindByValue((e.Row.FindControl("LL0") as Label).Text).Selected = true;
                 
            }
        
    }


    private DataTable GetData(SqlCommand cmd2)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["constring"]);
        //string strr = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
        //using (SqlConnection conn = new SqlConnection(strr))
        //{
            using (SqlDataAdapter ssda = new SqlDataAdapter())
            {
                cmd2.Connection = con;
                ssda.SelectCommand = cmd2;
                using (DataTable ddt = new DataTable())
                {
                    ssda.Fill(ddt);
                    return ddt;
                }


            //}
        }
    }


     
}





m得到这个错误 -

对象引用未设置为对象的实例。

ddrrpp.Items.FindByValue((e.Row.FindControl(LL0)作为标签)。文本).Selected = true;



plzz help ..



m getting this error-
Object reference not set to an instance of an object.
ddrrpp.Items.FindByValue((e.Row.FindControl("LL0") as Label).Text).Selected = true;

plzz help..

推荐答案

要么

either
(e.Row.FindControl("LL0") as Label)





返回null或





is returning null or

ddrrpp.Items.FindByValue((e.Row.FindControl("LL0") as Label).Text)





为空。在尝试调用其中一个属性之前,您应该检查它们是否为空。





is null. You should be checking for null on both of them before trying to call one of their properties.

Label label = (e.Row.FindControl("LL0") as Label);

if (label != null)
{
    ListItem item = ddrrpp.Items.FindByValue((e.Row.FindControl("LL0") as Label).Text);
    
    if (item != null)
    {
        item.Selected = true;
    }
}





这样可以防止页面崩溃,但不能保证页面不会崩溃以你认为应该的方式运作。这取决于GridView的设置方式。但是,您应该能够使用调试器逐步执行此操作并确定哪个对象为空。



This should keep your page from crashing but there''s no guarantee that it will operate in the way you think it should. That will depend on how your GridView is set up. However, you should be able to step through this using the debugger and determine which object is null.


这篇关于网格视图编辑项目模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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