如何在ASP.NET C#中的编辑模式下在网格视图列上显示日历控件 [英] How to display calendar control on grid view column on edit mode in ASP.NET C#

查看:64
本文介绍了如何在ASP.NET C#中的编辑模式下在网格视图列上显示日历控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个日历控件和1个Gridview控件。当用户从两个控件中选择日期时,我会在所选日期月份之间的gridview中显示数据。我为此编写了选择查询。我在搜索按钮上显示3个Colums FileName,UploadDate和ProcessedDate。

现在我要做的是我想在编辑时将日历控件添加到ProcessedDate列

 GridView1_RowEditing 

事件火灾。我可以在该文件中输入手动日期并保存到数据库中,但我想在那里显示日历控件。



这是我的aspx页面代码





< asp:GridView ID =GridView1runat =serverDataKeyNames =FileNameCellPadding =6 GridLines =NoneCssClass =inlineAutoGenerateEditButton =TrueOnRowEditing =GridView1_RowEditingOnRowUpdating =GridView1_RowUpdatingOnRowCancelingEdit =GridView1_RowCancelingEditBackColor =WhiteBorderColor =WhiteBorderStyle =RidgeBorderWidth =2px CellSpacing =1OnRowDataBound =GridView1_RowDataBound> 
< FooterStyle BackColor =#C6C3C6ForeColor =Black/>
< HeaderStyle BackColor =#4A3C8CFont-Bold =TrueForeColor =#E7E7FF/>
< PagerStyle BackColor =#C6C3C6ForeColor =BlackHorizo​​ntalAlign =Right/>
< RowStyle BackColor =#DEDFDEForeColor =Black/>
< SelectedRowStyle BackColor =#9471DEFont-Bold =TrueForeColor =White/>
< SortedAscendingCellStyle BackColor =#F1F1F1/>
< SortedAscendingHeaderStyle BackColor =#594B9C/>
< SortedDescendingCellStyle BackColor =#CAC9C9/>
< SortedDescendingHeaderStyle BackColor =#33276A/>


< / asp:GridView>





C#代码

 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

GridViewRow row = GridView1.Rows [e.RowIndex];
//日历textName =(日历)row.Cells [0] .Controls [0];
//System.Web.UI.WebControls.Calendar Calendar1 =(System.Web.UI.WebControls.Calendar)row.Cells [0] .Controls [0];
// TextBox ProcessedDate =(TextBox)row.FindControl(txtProductName);
// int PackageDetailsID = Convert.ToInt32(GridView1.DataKeys [e.RowIndex] .Values [0]);
string FileName = Convert.ToString(GridView1.DataKeys [e.RowIndex] .Values [0]);
string name =(row.Cells [2] .Controls [0] as TextBox).Text;
// string country =(row.Cells [3] .Controls [0] as TextBox).Text;
string constr = ConfigurationManager.ConnectionStrings [DBConnectionString]。ConnectionString;
using(SqlConnection con = new SqlConnection(constr))
{
using(SqlCommand cmd = new SqlCommand(UPDATE PackageDetails SET ProcessedDate = @ProcessedDate WHERE FileName = @FileName))
// using(SqlCommand cmd = new SqlCommand(UPDATE PackageDetails SET ProcessedDate ='+ Calendar1 +'WHERE FileName ='+ FileName +'))

{

cmd.Parameters.AddWithValue(@ FileName,FileName);
cmd.Parameters.AddWithValue(@ ProcessedDate,name);

cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
GridView1.EditIndex = -1;
//this.BindGrid();
this.BindDataGrid();

}

public void BindDataGrid()
{

string constr = ConfigurationManager.ConnectionStrings [DBConnectionString]。ConnectionString;
使用(SqlConnection con = new SqlConnection(constr))
{

using(SqlCommand cmd = new SqlCommand(SELECT DISTINCT FileName,UploadDate,ProcessedDate FROM PackageDetails WHERE CONVERT(DATE) ,UploadDate)BETWEEN @From AND @To,con))
{

using(SqlDataAdapter da = new SqlDataAdapter(cmd))
{
string date = ;
date = CalendarFrom.SelectedDate.ToString(MM-dd-yyyy);
string date2 =;
date2 = CalendarTo.SelectedDate.ToString(MM-dd-yyyy);
cmd.Parameters.Add(@ From,SqlDbType.Date).Value = date;
cmd.Parameters.Add(@ To,SqlDbType.Date).Value = date2;
DataSet ds = new DataSet();
da.Fill(ds);

GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}

}



 protected void GridView1_RowEditing(object sender,GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
//System.Web.UI.WebControls.Calendar Calendar1 =(System.Web.UI.WebControls.Calendar)row.Cells [0] .Controls [0];

//this.BindGrid();
this.BindDataGrid();
}





我尝试过:



i尝试使用javascript,但我没有得到结果,因为我期待。

解决方案

使用jquery日历检查这一点。



gridview中编辑项目模板(文本框)的JQuery Datepicker | ASP.NET论坛 [ ^ ]


谷歌搜索是你的朋友。使用您自己的CodeProject问题标题: ASP.NET C#编辑模式中gridview列上的日历控件 - Google [ ^ ]



在2秒内发现: asp.net - gridview中的日历编辑模式 - 堆栈溢出 [ ^

I have 2 calendar controls and 1 Gridview control.when user selects date from both controls i display data in the gridview between selected dates months. I have written select query for this. i am displaying 3 colums FileName,UploadDate And ProcessedDate on search button .
Now What i have to do is i want to add calendar control to ProcessedDate column when edit

GridView1_RowEditing

event fires. i am able to put manual date in that filed and save into the database but i want to display calendar control there.

this my aspx page code


<asp:GridView ID="GridView1" runat="server"  DataKeyNames="FileName" CellPadding="6" GridLines="None" CssClass="inline"  AutoGenerateEditButton="True" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellSpacing="1" OnRowDataBound="GridView1_RowDataBound">
             <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
             <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
             <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
             <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
             <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
             <SortedAscendingCellStyle BackColor="#F1F1F1" />
             <SortedAscendingHeaderStyle BackColor="#594B9C" />
             <SortedDescendingCellStyle BackColor="#CAC9C9" />
             <SortedDescendingHeaderStyle BackColor="#33276A" />


         </asp:GridView>



C# code

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
           
            GridViewRow row = GridView1.Rows[e.RowIndex];
               //Calendar textName = (Calendar)row.Cells[0].Controls[0];
            //System.Web.UI.WebControls.Calendar Calendar1 = (System.Web.UI.WebControls.Calendar)row.Cells[0].Controls[0];
            //TextBox ProcessedDate = (TextBox)row.FindControl("txtProductName");
            //int PackageDetailsID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
            string FileName = Convert.ToString(GridView1.DataKeys[e.RowIndex].Values[0]);
            string name = (row.Cells[2].Controls[0] as TextBox).Text;
            //string country = (row.Cells[3].Controls[0] as TextBox).Text;
            string constr = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand("UPDATE PackageDetails SET ProcessedDate = @ProcessedDate WHERE FileName = @FileName"))
                //using (SqlCommand cmd = new SqlCommand("UPDATE PackageDetails SET ProcessedDate = '" + Calendar1 + "' WHERE FileName ='" + FileName + "'"))

                {

                    cmd.Parameters.AddWithValue("@FileName", FileName);
                    cmd.Parameters.AddWithValue("@ProcessedDate", name);
                   
                    cmd.Connection = con;
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
            GridView1.EditIndex = -1;
            //this.BindGrid();
            this.BindDataGrid();

        }

        public void BindDataGrid()
        {

            string constr = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {

                using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT FileName, UploadDate, ProcessedDate FROM PackageDetails WHERE CONVERT(DATE, UploadDate) BETWEEN @From AND @To", con))
                {
                 
                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        string date = "";
                        date = CalendarFrom.SelectedDate.ToString("MM-dd-yyyy");
                        string date2 = "";
                        date2 = CalendarTo.SelectedDate.ToString("MM-dd-yyyy");
                        cmd.Parameters.Add("@From", SqlDbType.Date).Value = date;
                        cmd.Parameters.Add("@To", SqlDbType.Date).Value = date2;
                        DataSet ds = new DataSet();
                        da.Fill(ds);
                      
                        GridView1.DataSource = ds;
                        GridView1.DataBind();
                    }
                }
            }

        }


protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
       {
           GridView1.EditIndex = e.NewEditIndex;
          //System.Web.UI.WebControls.Calendar Calendar1 = (System.Web.UI.WebControls.Calendar)row.Cells[0].Controls[0];

           //this.BindGrid();
           this.BindDataGrid();
       }



What I have tried:

i have tried using javascript but i didn't get result as i am expecting.

解决方案

Check this, using jquery calendar.

JQuery Datepicker for edit item template (textbox) in gridview | The ASP.NET Forums[^]


Google search is your friend. Using you own CodeProject Question Title: calendar control on gridview column on edit mode in ASP.NET C# - Google[^]

In 2 seconds found this: asp.net - Calendar in gridview edit mode - Stack Overflow[^]


这篇关于如何在ASP.NET C#中的编辑模式下在网格视图列上显示日历控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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