更改用于插入新记录的文本 [英] Changing the Text for Inserting a New Record

查看:89
本文介绍了更改用于插入新记录的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



我正在尝试更改文本以插入新记录但是当我点击添加类别按钮时,我收到此错误消息:





InvalidCastException未被用户代码处理。



无法使用将'System.Web.UI.WebControls.TextBox'类型的对象强制转换为'System.Web.UI.WebControls.LinkBut​​ton'。



我的代码是:



< pre lang =c#> 
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControls;
使用System.Data;
使用System.Data.SqlClient;
使用System.Data.OleDb;
使用System.IO;
使用System.Data.Common;
使用System.Globalization;


命名空间GridView_Tutorial
{
公共部分类GridView:System.Web.UI.Page
{
protected void Page_Load(object sender ,EventArgs e)
{

}

protected void Button1_Click(object sender,EventArgs e)
{
SqlConnection con = new SqlConnection (数据源= Mehdi-PC \\SqlExpress;初始目录= PIMS;集成安全性=真);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(SELECT cat_id,cat_name FROM quest_categories,con);
DataTable dt = new DataTable();
da.Fill(dt);
//为返回的DataTable添加一个空行
DataRow dr = dt.NewRow();
dt.Rows.InsertAt(dr,0);
//创建第一行Gridview可编辑
GridView1.EditIndex = 0;

//数据采购和绑定
GridView1.DataSource = dt;
GridView1.DataBind();
//更改用于插入新记录的文本
((LinkBut​​ton)GridView1.Rows [0] .Cells [0] .Controls [0])。Text =Insert;
if(con!= null)
{
con.Close();
}
}
}
}





我错过了任何指令吗?或者它是别的东西。非常感谢您的帮助。



亲切问候

解决方案

更改



((LinkBut​​ton)GridView1.Rows [ 0 ]。单元格[ 0 ]。控件[ 0 ])。文本=  插入; 









((TextBox)GridView1.Rows [ 0 ]。单元格[ 0 ]。控件[ 0 ])。文本=  插入; 





您尝试设置.Text属性的控件不是LinkBut​​ton,这就是您获得的原因错误。您正在尝试将TextBox强制转换为LinkBut​​ton而您无法执行此操作。以上将修复错误,但是您可能尝试将控件设置在错误的单元格中,我将验证您的按钮是否在第一列中,否则更改单元格的索引以匹配链接按钮所在的列


Dear All,

I am trying the change the Text for INSERTING a new record but when I click 'Add Categories' button, I am getting this error message:


InvalidCastException was unhandled by the user code.

Unable to cast object of type 'System.Web.UI.WebControls.TextBox' to type 'System.Web.UI.WebControls.LinkButton'.

My code is:

<pre lang="c#">
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.Data.OleDb;
using System.IO;
using System.Data.Common;
using System.Globalization;


namespace GridView_Tutorial
{
    public partial class GridView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=Mehdi-PC\\SqlExpress; Initial Catalog=PIMS; Integrated Security=true");
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter("SELECT cat_id, cat_name FROM quest_categories", con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            //add a blank row to returned DataTable 
            DataRow dr = dt.NewRow();
            dt.Rows.InsertAt(dr, 0);
            //creating the first row of Gridview to be editable
            GridView1.EditIndex = 0;

            //Data Sourcing and binding
            GridView1.DataSource = dt;
            GridView1.DataBind();
            //Changing the Text for Inserting a New Record
            ((LinkButton)GridView1.Rows[0].Cells[0].Controls[0]).Text = "Insert";
            if (con != null)
            {
                con.Close();
            }
        }
    }
}



Am I missing any directives? or it is something else. your help is much appreciated.

Kind regards

解决方案

Change

((LinkButton)GridView1.Rows[0].Cells[0].Controls[0]).Text = "Insert";



to

((TextBox)GridView1.Rows[0].Cells[0].Controls[0]).Text = "Insert";



The control you are trying to set the .Text property on is not a LinkButton, which is why you are getting the error. You are attempting to cast a TextBox to a LinkButton and you can't do it. The above will fix the error, however you may be trying to set the control in the wrong cell, I'd verify that your button is in the first column, otherwise change the index of the cell to match the column the link button is in.


这篇关于更改用于插入新记录的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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