עבור לתוכן

בעייה ביצירת טור לחצנים לDataGrid בזמן ריצה

Featured Replies

פורסם

ניסיתי בזמן ריצה ליצור טור לחצנים לדאטא גריד, זה הצליח, הבעייה הייתה כשניסיתי לשייך אותם למתודה כלשהי, בדאטא גריד יש מתודה שנקראת

ItemCommand שאמורה להתבצע כל פעם שלוחצים על לחצן בדאטא גריד, אבל משום מה כשאני לוחץ על הלחצן בדאטא גריד שום דבר לא קורה ..

הוא רק נכנס לפייג' לוד וזהו .. מישהו נתקל אי פעם בבעיה?

פורסם

דבר ראשון, item command עובדת רק ללחצנים מסוג select (כשבוחרים לשים עמודה של כפתורים יש אפשרות לבחור סוג כפתור).

בכ"א, אחרי שיצרת את העמודה, כנס לproperties של הdatagrid, במסך הזה אתה תראה שיש ציור קטן של ברק צהוב למעלה (כשסמן העכבר מעליו תראה שכתוב events), עכשיו אתה תראה שם רשימה של כל מיני events שיכולים להתבצע.

תלחץ פעמיים על הevent שנקרא datagrid item command, ואז מסך התוכנה יעבוד מיד לדף הcode behind של דף הasp שעליו אתה עובד, ושים לב שנוספה לך מתודה חדשה.

המתודה תקרא:

private void datagrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)

בתוכה אתה רושם את הקוד שאתה רוצה שיעבוד.

פורסם
  • מחבר

מה הכוונה כפתורים מסוג סלקט?

עריכה:

אה הבנתי מה הכוונה כפתורים מסוג סלקט, אבל זה לא נכון, זה עובד לי עם כל סוגי הכפתורים אבל הבעיה היא רק בקטע שזה נוצר בזמן ריצה..

פורסם

הכפתורים של edit/delete/cancel הם כפתורים שקיימים רק בשביל מטרה מאוד ספציפית (בגלל שכאשר לוחצים עליהם הם משתנים וכו'), ולכן לא גמישים בפעולותיהם כמו הselect.

אז חוץ ממטרות מאוד מוגדות (למחוק משהו מהרשימה/לערוך אותו), אין סיבה להשתמש בהם.

מה ז"א שהבעיה היא שהלחצן נוצר בזמן ריצה?

ברור שהוא יווצר בזמן ריצה, הרי בשביל כל שורה בdatagrid נוצר כפתור, וכדי שהתוכנה תדע כמה כפתורים ליצור, ואיזה כפתור מתאים לאילו ערכים (בשורה שלו), צריך להריץ את הפרוייקט.

אם אתה רוצה לקבוע לכפתור מתודה כלשהי שתפעל בלחיצה עליו, אז כבר כתבתי לך איך לעשות את זה.

הרעיון הוא שהמתודה itemcommand הינה מתודה כללית כזאת, שלכל כפתור שילחץ, אותה מתודה תתבצע.

פורסם
  • מחבר

זמן ריצה הכוונה שלא ליצור אותם בדיזיין, אלא בזמן שהתוכנית תרוץ היא תתחיל ליצור את כל הטורים שבדאטא גריד.

במצב כזה הכפתורים לא פועלים עם המתודה של האייטם קומנד משום מה.. הנה קוד של מישהו שבנה דוגמא כזו

http://www.dotnet247.com/247reference/a.aspx?u=http://www.dotnetjunkies.com/HowTo/CEEE5469-58F8-4895-8822-76119BA2A575.dcik

הנה מה שאני עשיתי


[code=csharp]
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;

namespace ComputerStore
{
/// <summary>
/// Summary description for Catalog.
/// </summary>
public class Catalog : System.Web.UI.Page
{
protected System.Web.UI.WebControls.LinkButton LinkButtonShowProducts;
protected System.Web.UI.WebControls.LinkButton LinkButtonShowSuppliers;
protected System.Web.UI.WebControls.DataGrid DataGridCatalog;
protected System.Web.UI.WebControls.Panel PanelAdmin;
DataView dv;
string methodCall;
CatalogRef.Katalog c=new CatalogRef.Katalog();

private void Page_Load(object sender, System.EventArgs e)
{
/*if (!IsPostBack)
Session["dg"]=this.DataGridCatalog;
this.DataGridCatalog=(DataGrid)Session["dg"];*/
}

#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.LinkButtonShowProducts.Click += new System.EventHandler(this.LinkButtonShowProducts_Click);
this.LinkButtonShowSuppliers.Click += new System.EventHandler(this.LinkButtonShowSuppliers_Click);
this.DataGridCatalog.ItemCommand+=new DataGridCommandEventHandler(this.DirectTo);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void LinkButtonShowProducts_Click(object sender, System.EventArgs e)
{
Session["ds"]=c.ProductsOfKatalog();
Session["dv"]=new DataView(((DataSet)Session["ds"]).Tables[0]);
methodCall="ShowProducts";
PopulateDataGrid();
}

private void PopulateDataGrid()
{
dv=(DataView)(Session["dv"]);
for(int i=0;i<dv.Table.Columns.Count;i++)
{
BoundColumn bc=new BoundColumn();
bc.DataField=dv.Table.Columns[i].ColumnName;
this.DataGridCatalog.Columns.Add(bc);
}

if (methodCall=="ShowProducts")
{
ButtonColumn bc=new ButtonColumn();
bc.ButtonType=ButtonColumnType.LinkButton;
bc.Text="לספקים שמספקים את המוצר";
bc.CommandName="ShowSuppliersOfProducts";
this.DataGridCatalog.Columns.Add(bc);
this.DataGridCatalog.Columns[0].HeaderText="מספר מוצר";
this.DataGridCatalog.Columns[1].HeaderText="שם מוצר";
this.DataGridCatalog.Columns[2].HeaderText="סוג מוצר";
this.DataGridCatalog.Columns[3].HeaderText="חברה";
// this.DataGridCatalog.ItemCommand+=new DataGridCommandEventHandler(this.DirectTo);
}

if (methodCall=="ShowSuppliers")
{
ButtonColumn bc=new ButtonColumn();
bc.ButtonType=ButtonColumnType.LinkButton;
bc.Text="למוצרים שהספק מספק";
bc.CommandName="ShowProductsOfSupplier";
this.DataGridCatalog.Columns.Add(bc);
this.DataGridCatalog.Columns[0].HeaderText="מספר ספק";
this.DataGridCatalog.Columns[1].HeaderText="שם ספק";
this.DataGridCatalog.Columns[2].HeaderText="אימייל";
this.DataGridCatalog.Columns[3].HeaderText="כתובת";
this.DataGridCatalog.Columns[4].HeaderText="טלפון";

}
if (methodCall=="SuppliersOfProducts")
{
}

DataGridCatalog.DataSource=dv;
DataGridCatalog.DataBind();
}

public void DirectTo(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if (e.CommandName=="ShowSuppliersOfProducts")
{
Session["ds"]=c.GetProductsOfSupplier(int.Parse(((Label)(e.Item.Cells[0].Controls[0])).Text));
Session["dv"]=new DataView(((DataSet)Session["ds"]).Tables[0]);
methodCall="SuppliersOfProducts";
PopulateDataGrid();
}
}

private void LinkButtonShowSuppliers_Click(object sender, System.EventArgs e)
{
Session["ds"]=c.SuppliersOfKatalog();
Session["dv"]=new DataView(((DataSet)Session["ds"]).Tables[0]);
methodCall="ShowSuppliers";
PopulateDataGrid();
}



}
}

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים