无法在gridview中写入 [英] not able to write in the gridview

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

问题描述

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CreateChallan.aspx.cs" Inherits="CreateChallan" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server"

                AllowPaging="True"

                DataSourceID="SqlDataSource1"

                AutoGenerateColumns="false">
                <Columns>

                    <asp:TemplateField HeaderText="Select">


                        <ItemTemplate>
                            <asp:CheckBox ID="chkSelect" runat="server"

                                AutoPostBack="true"

                                OnCheckedChanged="chkSelect_CheckedChanged" />
                        </ItemTemplate>

                    </asp:TemplateField>
                    <asp:BoundField DataField="DocketNo" HeaderText="DocketNo"

                        SortExpression="ID" />

                    <asp:TemplateField HeaderText="BranchCode"

                        SortExpression="BranchCode">
                        <ItemTemplate>
                            <asp:TextBox ID="txtBranchCode" runat="server"

                                Text='<%# Bind("BranchCode") %>' ReadOnly="true"

                                ForeColor="Blue" BorderStyle="none"

                                BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>


                    <asp:TemplateField HeaderText="Date"

                        SortExpression="Date">
                        <ItemTemplate>
                            <asp:TextBox ID="txtDate" runat="server"

                                Text='<%# Bind("Date") %>'

                                ReadOnly="true" ForeColor="Blue"

                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                        <asp:TemplateField HeaderText="PKTS"

                        SortExpression="PKTS">
                        <ItemTemplate>
                            <asp:TextBox ID="txtPKTS" runat="server"

                                Text='<%# Bind("PKTS") %>' ReadOnly="true"

                                ForeColor="Blue" BorderStyle="none"

                                BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="To"

                        SortExpression="To">
                        <ItemTemplate>
                            <asp:TextBox ID="txtTo" runat="server"

                                Text='<%# Bind("To") %>'

                                ReadOnly="true" ForeColor="Blue"

                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>



                    <asp:TemplateField HeaderText="ActWT"

                        SortExpression="ActWt">
                        <ItemTemplate>
                            <asp:TextBox ID="txtActWt" runat="server"

                                Text='<%# Bind("ActWt") %>'

                                ReadOnly="true" ForeColor="Blue"

                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                     <asp:TemplateField HeaderText="ChargeWt"

                        SortExpression="ChargeWt">
                        <ItemTemplate>
                            <asp:TextBox ID="txtChargeWt" runat="server"

                                Text='<%# Bind("ChargeWt") %>'

                                ReadOnly="true" ForeColor="Blue"

                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                     <asp:TemplateField HeaderText="Mode"

                        SortExpression="Mode">
                        <ItemTemplate>
                            <asp:TextBox ID="Mode" runat="server"

                                Text='<%# Bind("Mode") %>'

                                ReadOnly="true" ForeColor="Blue"

                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                     <asp:TemplateField HeaderText="ChallanNo"

                        SortExpression="ChallanNo">
                        <ItemTemplate>
                            <asp:TextBox ID="txtChallanNo" runat="server"

                                Text='<%# Bind("ChallanNo") %>'

                                ReadOnly="true" ForeColor="Blue"

                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                     <asp:TemplateField HeaderText="ChallanDate"

                        SortExpression="ChallanDate">
                        <ItemTemplate>
                            <asp:TextBox ID="txtChallanDate" runat="server"

                                Text='<%# Bind("ChallanDate") %>'

                                ReadOnly="true" ForeColor="Blue"

                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                     <asp:TemplateField HeaderText="VehicleNo"

                        SortExpression="VehicleNo">
                        <ItemTemplate>
                            <asp:TextBox ID="VehicleNo" runat="server"

                                Text='<%# Bind("VehicleNo") %>'

                                ReadOnly="true" ForeColor="Blue"

                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                     <asp:TemplateField HeaderText="Description"

                        SortExpression="Description">
                        <ItemTemplate>
                            <asp:TextBox ID="txtDescription" runat="server"

                                Text='<%# Bind("Description") %>'

                                ReadOnly="true" ForeColor="Blue"

                                BorderStyle="none" BorderWidth="0px">
                            </asp:TextBox>
                        </ItemTemplate>
                         </asp:TemplateField>




                </Columns>
            </asp:GridView>
            SELECT FROM
            <asp:SqlDataSource ID="SqlDataSource1" runat="server"

                ConnectionString="<%$ ConnectionStrings:amitpandeyConnectionString2 %>"

                SelectCommand="SELECT [Date], [DocketNo], [To], [PKTS], [ActWt], [ChargeWt], [Mode], [BranchCode], [VehicleNo],
             [ChallanDate], [Description], [ChallanNo]  FROM [CreateDocket]"

                DeleteCommand="DELETE FROM product WHERE (DocketNo= @ID)"

                UpdateCommand="UPDATE [CreateDocket] SET [To] = @To,
              [Date] = @Date,[PKTS]=@PKTS,[BranchCode] = @BranchCode,[ActWt] = @ActWt,[ChargeWt]=@ChargeWt,[Mode]=@Mode,[ChallanNo]=@ChallanNo,[ChallanDate]=@ChallanDate,[VehicleNo]=@VehicleNo,[Description]=@Description WHERE [ID] = @ID">
                <DeleteParameters>
                    <asp:Parameter Name="ID" />
                </DeleteParameters>
                <UpdateParameters>
                    <asp:Parameter Name="Date" />
                    <asp:Parameter Name="PKTS" />
                    <asp:Parameter Name="To" />
                    <asp:Parameter Name="BranchCode" />
                    <asp:Parameter Name="ActWt" />
                    <asp:Parameter Name="ChargeWT" />
                    <asp:Parameter Name="Mode" />
                     <asp:Parameter Name="ChallanNo" />
                    <asp:Parameter Name="ChallanDate" />
                    <asp:Parameter Name="VehicleNo" />
                    <asp:Parameter Name="Description" />

                </UpdateParameters>
            </asp:SqlDataSource>
            <br />

            <asp:Button ID="btnUpdate" runat="server"

                OnClick="btnUpdate_Click" Text="Save" /><br />


        </div>
    </form>
</body>
</html>





and the cs code is



and the cs code is

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text;

public partial class CreateChallan : System.Web.UI.Page
{
    string connStr = ConfigurationManager.ConnectionStrings["amitpandeyConnectionString2"].ConnectionString;
      protected void btnUpdate_Click(object sender, EventArgs e)
        {
            StringBuilder strSql = new StringBuilder(string.Empty);

            SqlConnection con = new SqlConnection(connStr);
            SqlCommand cmd = new SqlCommand();

            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox chkUpdate = (CheckBox)
                   GridView1.Rows[i].Cells[0].FindControl("chkSelect");
                if (chkUpdate != null)
                {
                    if (chkUpdate.Checked)
                    {

                        string strDocketNo = GridView1.Rows[i].Cells[1].Text;
                        string strBranchCode = ((TextBox)
                            GridView1.Rows[i].FindControl("txtBranchCode")).Text;

                        string strDate = ((TextBox)
                            GridView1.Rows[i].FindControl("txtDate")).Text;

                        string strPKTS = ((TextBox)
                            GridView1.Rows[i].FindControl("txtPKTS")).Text;

                        string strActWt = ((TextBox)
                            GridView1.Rows[i].FindControl("txtActwt")).Text;

                        string strChargeWt = ((TextBox)
                            GridView1.Rows[i].FindControl("txtChargeWt")).Text;

                        string strMode = ((TextBox)
                            GridView1.Rows[i].FindControl("Mode")).Text;

                        string strChallanNo = ((TextBox)
                            GridView1.Rows[i].FindControl("ChallanNo")).Text;

                        string strChallanDate = ((TextBox)
                            GridView1.Rows[i].FindControl("ChallanDate")).Text;

                        string strvehicleNo = ((TextBox)
                            GridView1.Rows[i].FindControl("VehicleNo")).Text;

                        string strDescription = ((TextBox)
                            GridView1.Rows[i].FindControl("Description")).Text;

                       


                        string strUpdate =
                            "Update CreateDocket set BranchCode = '" + strBranchCode + "',Date = '" + strDate + "',PKTS = '" + strPKTS + "',ActWt = '" + strActWt + "',ChargeWt = '" + strChargeWt + "',Mode = '" + strMode + "',ChallanNo = '" + strChallanNo + "',ChallanDate = '" + strChallanDate + "',VehicleNo = '" + strvehicleNo + "',Description = '" + strDescription + "' WHERE DEocketNo ='" +strDocketNo  + "'";

                        strSql.Append(strUpdate);
                    }
                }
            }
            try
            {
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = strSql.ToString();
                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                string errorMsg = "Error in Updation";
                errorMsg += ex.Message;
                throw new Exception(errorMsg);
            }
            finally
            {
                con.Close();
            }
            
        }
       
        protected void chkSelect_CheckedChanged
                            (object sender, EventArgs e)
        {
            CheckBox chkTest = (CheckBox)sender;
            GridViewRow grdRow = (GridViewRow)chkTest.NamingContainer;
            TextBox txtBranchCode = (TextBox)grdRow.FindControl
                                                ("txtBranchCode");
            TextBox txtDate = (TextBox)grdRow.FindControl
                                              ("txtDate");

            TextBox txtPKTS = (TextBox)grdRow.FindControl
                                              ("txtPKTS");

            TextBox txtActWT = (TextBox)grdRow.FindControl
                                              ("txtActWT");

            TextBox txtChargeWt = (TextBox)grdRow.FindControl
                                              ("txtChargeWt");

            TextBox txtMode = (TextBox)grdRow.FindControl
                                              ("txtMode");

            TextBox txtChallanNo = (TextBox)grdRow.FindControl
                                              ("txtChallanNo");

            TextBox txtChallanDate = (TextBox)grdRow.FindControl
                                              ("txtChallanDate");

            TextBox txtVehicleNo = (TextBox)grdRow.FindControl
                                              ("txtVehicleNo");

            TextBox txtDescription = (TextBox)grdRow.FindControl
                                              ("txtDescription");

            if (chkTest.Checked)
            {
              
                txtChallanNo.ReadOnly = false;
                txtChallanDate.ReadOnly = false;
                txtVehicleNo.ReadOnly = false;
                txtDescription.ReadOnly = false;
                txtChallanNo.ForeColor = System.Drawing.Color.Red;
                txtChallanDate.ForeColor = System.Drawing.Color.Red;
                txtVehicleNo.ForeColor = System.Drawing.Color.Red;
                txtDescription.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                txtChallanNo.ReadOnly = true;
                txtChallanDate.ReadOnly = true;
                txtVehicleNo.ReadOnly = true;
                txtDescription.ReadOnly = true;
                txtChallanNo.ForeColor = System.Drawing.Color.Green;
                txtChallanDate.ForeColor = System.Drawing.Color.Green;
                txtVehicleNo.ForeColor = System.Drawing.Color.Green;
                txtDescription.ForeColor = System.Drawing.Color.Green;  
                
            }
        }
    }



my problem is I want to write in challanNo,ChallanDate,VehicleNo,Description but every thing is working fine except I am not able to write inside challanNo,ChallanDate,VehicleNo,Description PLEASE HELP ME.


my problem is I want to write in challanNo,ChallanDate,VehicleNo,Description but every thing is working fine except I am not able to write inside challanNo,ChallanDate,VehicleNo,Description PLEASE HELP ME.

推荐答案

ConnectionStrings:amitpandeyConnectionString2 %>\"

SelectCommand=\"SELECT [Date], [DocketNo], [To], [PKTS], [ActWt], [ChargeWt], [Mode], [BranchCode], [VehicleNo],
[ChallanDate], [Description], [ChallanNo] FROM [CreateDocket]\"


DeleteCommand=\"DELETE FROM product WHERE (DocketNo= @ID)\"

UpdateCommand=\"UPDATE [CreateDocket] SET [To] = @To,
[Date] = @Date,[PKTS]=@PKTS,[BranchCode] = @BranchCode,[ActWt] = @ActWt,[ChargeWt]=@ChargeWt,[Mode]=@Mode,[ChallanNo]=@ChallanNo,[ChallanDate]=@ChallanDate,[VehicleNo]=@VehicleNo,[Description]=@Description WHERE [ID] = @ID\"
>
<DeleteParameters>
<asp:Parameter Name=\"ID\" />
</DeletePara meters>
<UpdateParameters>
<asp:Parameter Name=\"Date\" />
<asp:Parameter Name=\"PKTS\" />
<asp:Parameter Name=\"To\" />
<asp:Parameter Name=\"BranchCode\" />
<asp:Parameter Name=\"ActWt\" />
<asp:Parameter Name=\"ChargeWT\" />
<asp:Parameter Name=\"Mode\" />
<asp:Parameter Name=\"ChallanNo\" />
<asp:Parameter Name=\"ChallanDate\" />
<asp:Parameter Name=\"VehicleNo\" />
<asp:Parameter Name=\"Description\" />

</UpdateParameters>
</asp:SqlDataSource>
<br />

<asp:Button ID=\"btnUpdate\" runat=\"server\"

OnClick=\"btnUpdate_Click\" Text=\"Save\" /><br />


</div>
</form>
</body>
</html>
ConnectionStrings:amitpandeyConnectionString2 %>" SelectCommand="SELECT [Date], [DocketNo], [To], [PKTS], [ActWt], [ChargeWt], [Mode], [BranchCode], [VehicleNo], [ChallanDate], [Description], [ChallanNo] FROM [CreateDocket]" DeleteCommand="DELETE FROM product WHERE (DocketNo= @ID)" UpdateCommand="UPDATE [CreateDocket] SET [To] = @To, [Date] = @Date,[PKTS]=@PKTS,[BranchCode] = @BranchCode,[ActWt] = @ActWt,[ChargeWt]=@ChargeWt,[Mode]=@Mode,[ChallanNo]=@ChallanNo,[ChallanDate]=@ChallanDate,[VehicleNo]=@VehicleNo,[Description]=@Description WHERE [ID] = @ID"> <DeleteParameters> <asp:Parameter Name="ID" /> </DeleteParameters> <UpdateParameters> <asp:Parameter Name="Date" /> <asp:Parameter Name="PKTS" /> <asp:Parameter Name="To" /> <asp:Parameter Name="BranchCode" /> <asp:Parameter Name="ActWt" /> <asp:Parameter Name="ChargeWT" /> <asp:Parameter Name="Mode" /> <asp:Parameter Name="ChallanNo" /> <asp:Parameter Name="ChallanDate" /> <asp:Parameter Name="VehicleNo" /> <asp:Parameter Name="Description" /> </UpdateParameters> </asp:SqlDataSource> <br /> <asp:Button ID="btnUpdate" runat="server" OnClick="btnUpdate_Click" Text="Save" /><br /> </div> </form> </body> </html>





and the cs code is



and the cs code is

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text;

public partial class CreateChallan : System.Web.UI.Page
{
    string connStr = ConfigurationManager.ConnectionStrings["amitpandeyConnectionString2"].ConnectionString;
      protected void btnUpdate_Click(object sender, EventArgs e)
        {
            StringBuilder strSql = new StringBuilder(string.Empty);

            SqlConnection con = new SqlConnection(connStr);
            SqlCommand cmd = new SqlCommand();

            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox chkUpdate = (CheckBox)
                   GridView1.Rows[i].Cells[0].FindControl("chkSelect");
                if (chkUpdate != null)
                {
                    if (chkUpdate.Checked)
                    {

                        string strDocketNo = GridView1.Rows[i].Cells[1].Text;
                        string strBranchCode = ((TextBox)
                            GridView1.Rows[i].FindControl("txtBranchCode")).Text;

                        string strDate = ((TextBox)
                            GridView1.Rows[i].FindControl("txtDate")).Text;

                        string strPKTS = ((TextBox)
                            GridView1.Rows[i].FindControl("txtPKTS")).Text;

                        string strActWt = ((TextBox)
                            GridView1.Rows[i].FindControl("txtActwt")).Text;

                        string strChargeWt = ((TextBox)
                            GridView1.Rows[i].FindControl("txtChargeWt")).Text;

                        string strMode = ((TextBox)
                            GridView1.Rows[i].FindControl("Mode")).Text;

                        string strChallanNo = ((TextBox)
                            GridView1.Rows[i].FindControl("ChallanNo")).Text;

                        string strChallanDate = ((TextBox)
                            GridView1.Rows[i].FindControl("ChallanDate")).Text;

                        string strvehicleNo = ((TextBox)
                            GridView1.Rows[i].FindControl("VehicleNo")).Text;

                        string strDescription = ((TextBox)
                            GridView1.Rows[i].FindControl("Description")).Text;

                       


                        string strUpdate =
                            "Update CreateDocket set BranchCode = '" + strBranchCode + "',Date = '" + strDate + "',PKTS = '" + strPKTS + "',ActWt = '" + strActWt + "',ChargeWt = '" + strChargeWt + "',Mode = '" + strMode + "',ChallanNo = '" + strChallanNo + "',ChallanDate = '" + strChallanDate + "',VehicleNo = '" + strvehicleNo + "',Description = '" + strDescription + "' WHERE DEocketNo ='" +strDocketNo  + "'";

                        strSql.Append(strUpdate);
                    }
                }
            }
            try
            {
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = strSql.ToString();
                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                string errorMsg = "Error in Updation";
                errorMsg += ex.Message;
                throw new Exception(errorMsg);
            }
            finally
            {
                con.Close();
            }
            
        }
       
        protected void chkSelect_CheckedChanged
                            (object sender, EventArgs e)
        {
            CheckBox chkTest = (CheckBox)sender;
            GridViewRow grdRow = (GridViewRow)chkTest.NamingContainer;
            TextBox txtBranchCode = (TextBox)grdRow.FindControl
                                                ("txtBranchCode");
            TextBox txtDate = (TextBox)grdRow.FindControl
                                              ("txtDate");

            TextBox txtPKTS = (TextBox)grdRow.FindControl
                                              ("txtPKTS");

            TextBox txtActWT = (TextBox)grdRow.FindControl
                                              ("txtActWT");

            TextBox txtChargeWt = (TextBox)grdRow.FindControl
                                              ("txtChargeWt");

            TextBox txtMode = (TextBox)grdRow.FindControl
                                              ("txtMode");

            TextBox txtChallanNo = (TextBox)grdRow.FindControl
                                              ("txtChallanNo");

            TextBox txtChallanDate = (TextBox)grdRow.FindControl
                                              ("txtChallanDate");

            TextBox txtVehicleNo = (TextBox)grdRow.FindControl
                                              ("txtVehicleNo");

            TextBox txtDescription = (TextBox)grdRow.FindControl
                                              ("txtDescription");

            if (chkTest.Checked)
            {
              
                txtChallanNo.ReadOnly = false;
                txtChallanDate.ReadOnly = false;
                txtVehicleNo.ReadOnly = false;
                txtDescription.ReadOnly = false;
                txtChallanNo.ForeColor = System.Drawing.Color.Red;
                txtChallanDate.ForeColor = System.Drawing.Color.Red;
                txtVehicleNo.ForeColor = System.Drawing.Color.Red;
                txtDescription.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                txtChallanNo.ReadOnly = true;
                txtChallanDate.ReadOnly = true;
                txtVehicleNo.ReadOnly = true;
                txtDescription.ReadOnly = true;
                txtChallanNo.ForeColor = System.Drawing.Color.Green;
                txtChallanDate.ForeColor = System.Drawing.Color.Green;
                txtVehicleNo.ForeColor = System.Drawing.Color.Green;
                txtDescription.ForeColor = System.Drawing.Color.Green;  
                
            }
        }
    }



my problem is I want to write in challanNo,ChallanDate,VehicleNo,Description but every thing is working fine except I am not able to write inside challanNo,ChallanDate,VehicleNo,Description PLEASE HELP ME.


my problem is I want to write in challanNo,ChallanDate,VehicleNo,Description but every thing is working fine except I am not able to write inside challanNo,ChallanDate,VehicleNo,Description PLEASE HELP ME.


Hi,



As I understand your problem



kindly remove ReadOnly=\"true\" Property



Use like below



<ItemTemplate>

<asp:TextBox CssClass=\"txtbox\" ID=\"txtId runat=\"server\" value=’<%#Container.DataItem(\"Value\") %>’ ></asp:TextBox>

</ItemTemplate>





Try thisasp:TextBox>







Try this.
Hi,

As I understand your problem

kindly remove ReadOnly="true" Property

Use like below

<ItemTemplate>
<asp:TextBox CssClass="txtbox" ID="txtId runat="server" value='<%#Container.DataItem("Value") %>' ></asp:TextBox>
</ItemTemplate>


Try thisasp:TextBox>



Try this.


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

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