在Radgrid 使用更RadColourPicker

  • 85
  • 0

在Radgrid 使用更RadColourPicker

WebForm1

	<telerik:RadColorPicker ID="RadColorPickerEdit" runat="server" SelectedColor='<%# System.Drawing.ColorTranslator.FromHtml(DataBinder.Eval(Container.DataItem, "FavoriteColor", "{0}")) %>'
								ShowIcon="true" PreviewColor="true" Style="float: left;" CssClass="filed" />

WebForm.cs

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


        RadColorPicker colorPicker;



        protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem dataItem = e.Item as GridDataItem;
                GridTableCell cell = dataItem["Colour"] as GridTableCell;

                if   ( (string.IsNullOrEmpty(dataItem["Colour"].Text)) || dataItem["Colour"].Text == "&nbsp;")
                {
                    cell.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFFFF");
                }
                else
                {
                    cell.BackColor = System.Drawing.ColorTranslator.FromHtml(dataItem["Colour"].Text);
                    //Response.Write(": " +dataItem["Colour"].Text);
                }
                
            }
        }

        protected void SqlDataSource1_Updating(object sender, SqlDataSourceCommandEventArgs e)
        {
            e.Command.Parameters["@ModifyDate"].Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
            e.Command.Parameters["@ModifyBy"].Value = Convert.ToString(HttpContext.Current.Session["EmplNo"]);

            string color = System.Drawing.ColorTranslator.ToHtml(colorPicker.SelectedColor);//Get the selected color and convert it to text
            SqlParameter strParam = new SqlParameter("@Colour", color);
            e.Command.Parameters.Add(strParam);// Pass the selected color as text to the server 
        }

        protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e)
        {
            e.Command.Parameters["@Modifydate"].Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
            e.Command.Parameters["@CreateDate"].Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm");

            e.Command.Parameters["@CreateBy"].Value = Convert.ToString(HttpContext.Current.Session["EmplNo"]);
            e.Command.Parameters["@ModifyBy"].Value = Convert.ToString(HttpContext.Current.Session["EmplNo"]);

            string color = System.Drawing.ColorTranslator.ToHtml(colorPicker.SelectedColor);//Get the selected color and convert it to text
            SqlParameter strParam = new SqlParameter("@Colour", color);
            e.Command.Parameters.Add(strParam);// Pass the selected color as text to the server 
        }



        protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            RadGrid grid = sender as RadGrid;
            colorPicker = e.Item.FindControl("RadColorPickerEdit") as RadColorPicker;// Get the RadColorPicker object
        }

        protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
        {
            RadGrid grid = sender as RadGrid;
            colorPicker = e.Item.FindControl("RadColorPickerEdit") as RadColorPicker;// Get the RadColorPicker object
        }
    }
}