Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Разработка под ASP.NET > Не выбирается строка в DataGrid


Автор: HalkaR 8.5.2005, 16:17
Код

<%@ Page language="c#" Codebehind="template_view.aspx.cs" AutoEventWireup="false" Inherits="cms.template_view" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <title>template_view</title>
        <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
        <meta content="C#" name="CODE_LANGUAGE">
        <meta content="JavaScript" name="vs_defaultClientScript">
        <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
        <form id="Form1" method="post" runat="server">
            <TABLE id="Table1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" cellSpacing="1"
                cellPadding="1" width="50%" border="0">
                <TR>
                    <TD>Name</TD>
                    <TD><%# TemplateName %></TD>
                </TR>
                <TR>
                    <TD>Code</TD>
                    <TD><%# TemplateCode %></TD>
                </TR>
                <TR>
                    <TD>Creation</TD>
                    <TD><%# CreationDate %></TD>
                </TR>
                <TR>
                    <TD>Modify</TD>
                    <TD><%# ModifyDate %></TD>
                </TR>
            </TABLE>
            <asp:DataGrid id="FieldsGrid" style="Z-INDEX: 102; LEFT: 16px; POSITION: absolute; TOP: 112px"
                runat="server" Width="312px" Height="120px" AutoGenerateColumns="False">
                <Columns>
                    <asp:BoundColumn DataField="FieldCode" HeaderText="Field Code"></asp:BoundColumn>
                    <asp:BoundColumn DataField="FieldName" HeaderText="Field Name"></asp:BoundColumn>
                    <asp:BoundColumn DataField="FieldTypeID" HeaderText="Field Type"></asp:BoundColumn>
                    <asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Change" ButtonType="PushButton"></asp:EditCommandColumn>
                </Columns>
            </asp:DataGrid>
        </form>
    </body>
</HTML>



Код

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace cms
{
    /// <summary>
    /// Summary description for template_view.
    /// </summary>
    public class template_view : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.DataGrid DataGrid1;        
        public string TemplateID;
        public SqlConnection Connection;
        public db sql = new db();
        public string TemplateName;
        public string TemplateCode;
        public string CreationDate;
        protected System.Web.UI.WebControls.DataGrid DataGrid2;
        protected System.Web.UI.WebControls.DataGrid FieldsGrid;
        public string ModifyDate;

        private void Page_Load(object sender, System.EventArgs e)
        {
            Connection = sql.Connect();
            TemplateID=Request.QueryString.GetValues(0).GetValue(0).ToString();
            
            SqlCommand cmd = new SqlCommand("SELECT TemplateName, TemplateCode, CreationDate, ModifyDate FROM x_templates WHERE TemplateID="+TemplateID, Connection);
            SqlDataReader reader = cmd.ExecuteReader();
            reader.Read();
            TemplateName=reader["TemplateName"].ToString();
            TemplateCode=reader["TemplateCode"].ToString();
            CreationDate=reader["CreationDate"].ToString();
            ModifyDate=reader["ModifyDate"].ToString();            
            reader.Close();

            FieldsGrid.DataSource = this.GetDataSet().Tables["fields"];
            //FieldsGrid.EditItemIndex = 1;

            this.DataBind();    

        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }
        
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {    
            this.FieldsGrid.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.FieldsGrid_CancelCommand);
            this.FieldsGrid.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.FieldsGrid_EditCommand);
            this.FieldsGrid.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.FieldsGrid_UpdateCommand);
            this.FieldsGrid.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.FieldsGrid_DeleteCommand);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion


        public DataSet GetDataSet()
        {
            SqlDataAdapter da = new SqlDataAdapter("SELECT FieldCode, FieldName, FieldTypeID FROM x_template_fields WHERE TemplateID="+TemplateID, Connection);
            DataSet ds = new DataSet();
            da.Fill(ds, "fields");
            return ds;
        }

        private void FieldsGrid_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            FieldsGrid.EditItemIndex = e.Item.ItemIndex;
            
            FieldsGrid.DataSource = this.GetDataSet().Tables["fields"];            

            FieldsGrid.DataBind();
        }

        private void FieldsGrid_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
        
        }

        private void FieldsGrid_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
        
        }

        private void FieldsGrid_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
        
        }

    }
}



почему при нажатии Edit не выбирается строка?

Автор: Kurt 8.5.2005, 21:34
В behind-коде в ф-ции Page_Load поменяй
Код
this.DataBind();

на
Код
if (!IsPostBack)
{
this.DataBind();
}


и будет тебе счастье.

Автор: HalkaR 9.5.2005, 09:09
Спасибо! заработало

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)