2011-11-23 82 views
0

好吧,這可能是一個黑暗中的鏡頭,因爲很難猜測這裏發生了什麼。但我在這裏沒有選擇。Visual Studio用戶控件ASP,C#,代碼後面的問題

所以我有這樣的代碼隱藏在頁面中,用戶可以填寫頁面上的輸入字段,然後通過單擊提交按鈕將信息添加到數據庫中。這工作100%。但是,我想將此功能放在用戶控件中,以便在不同的頁面上使用它,但效果相同。不過,我無法得到這個工作。

因此,這裏的工作code

的btnSubmit_Click方法,將信息添加到數據庫沒有問題。

現在爲用戶user control code。這沒有做任何事情。我做的唯一區別是它使用了一個ASPImageButton,我原本只是一個普通的ASPButton,但並沒有改變。

所以我說在黑暗中的一槍。如果任何人有什麼嘗試或修復的建議,請讓我知道。如果需要,我可以提供更多信息。

工作代碼:

using System; 
using System.Collections.Generic; 
using System.Data; 
using System.Data.Linq; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

using SenProPOS.Data; 
using SenProPOS.Web.Classes; 

namespace SenProPOS.Web.Admin.Pages 
{ 
public partial class InventoryMaintenance : BasePage 
{ 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      BindInventoryEntries(); 
     } 
    } 

    #region Properties 

    protected int CurrentInventoryID 
    { 
     get { return ViewState["CurrentInventoryID"] == null ? -1 : Convert.ToInt32(ViewState["CurrentInventoryID"].ToString()); } 
     set { ViewState["CurrentInventoryID"] = value; } 
    } 

    protected int CurrentInventoryMaintPage 
    { 
     get { return ViewState["CurrentInventoryMaintPage"] == null ? 1 : Convert.ToInt32(ViewState["CurrentInventoryMaintPage"].ToString()); } 
     set { ViewState["CurrentInventoryMaintPage"] = value; } 
    } 

    protected int InventoryEntriesPerPage 
    { 
     get { return Convert.ToInt32(ViewState["InventoryEntriesPerPage"] as String ?? "25"); } 
     set { ViewState["InventoryEntriesPerPage"] = value; } 
    } 


    #endregion 

    #region Methods 

    private void BindInventoryEntries() 
    { 
     try 
     { 

      using (SenProDataDataContext context = new SenProDataDataContext()) 
      { 
       var inventories = context.Inventory_Items.ToList(); 

       String search = tbInventorySearch.Text.Trim().ToLower(); 
       if (!String.IsNullOrEmpty(search)) 
       { 
        inventories = inventories.Where(x => x.Name.ToLower().Contains(search) 
         || x.Description.ToLower().Contains(search.ToLower()) 
         || x.UPC == Convert.ToInt32(search) 
         || x.Quantity == Convert.ToInt32(search) 
         || (double)x.Price == Convert.ToDouble(search) 
         || (double)x.Cost == Convert.ToDouble(search)) 
         .ToList(); 
       } 

       lvInventories.DataSource = inventories; 
       lvInventories.DataBind(); 

       if (String.IsNullOrEmpty(this.lvInventories.SortExpression)) 
       { 
        lvInventories.Sort("Name", SortDirection.Descending); 
       } 

       /** 
       var departments = context.Departments.ToList(); 
       this.ddlDepartment.DataSource = departments; 
       this.ddlDepartment.DataValueField = "ID"; 
       this.ddlDepartment.DataTextField = "Name"; 
       this.ddlDepartment.DataBind(); 

       var categories = context.Categories.ToList(); 
       this.ddlCategory.DataSource = categories; 
       this.ddlCategory.DataValueField = "ID"; 
       this.ddlCategory.DataTextField = "Name"; 
       this.ddlCategory.DataBind(); 
       * **/ 

      } 
     } 
     catch (Exception ex) 
     { 
      ; 
     } 
    } 

    private void InventoryEntrySelected(int InventoryID) 
    { 
     CurrentInventoryID = InventoryID; 

     this.tbName.Text = String.Empty; 
     this.tbUPC.Text = String.Empty; 
     this.tbDescription.Text = String.Empty; 
     this.tbQuantity.Text = String.Empty; 
     this.tbPricePerUnit.Text = String.Empty; 
     this.tbCostPerUnit.Text = String.Empty; 
     this.ddlDepartment.SelectedIndex = -1; 
     this.ddlCategory.SelectedIndex = -1; 

     if (CurrentInventoryID != -1) 
     { 
      using (SenProDataDataContext context = new SenProDataDataContext()) 
      { 
       var inventory = context.Inventory_Items.SingleOrDefault(x => x.ID == CurrentInventoryID); 
       if (inventory != null) 
       { 
        this.tbName.Text = inventory.Name; 
        this.tbUPC.Text = inventory.UPC.ToString(); 
        this.tbDescription.Text = inventory.Description; 
        this.tbQuantity.Text = inventory.Quantity.ToString(); 
        this.tbPricePerUnit.Text = inventory.Price.ToString(); 
        this.tbCostPerUnit.Text = inventory.Cost.ToString(); 

        /** needs fixing yet 
        var department = this.ddlDepartment.Items.FindByValue(inventory..ToString()); 
        if (department != null) 
        { 
         department.Selected = true; 
        } 

        var category = this.ddlCategories.Items.FindByValue(inventory.Category.ToString()); 
        if (position != null) 
        { 
         position.Selected = true; 
        } 

        var category = this.ddlSuppliers.Items.FindByValue(inventory.Category.ToString()); 
        if (supplier != null) 
        { 
         supplier.Selected = true; 
        } 

        **/ 
       } 
       else throw new ApplicationException("The specified item was not found."); 
      } 
     } 
    } 

    #endregion 

    #region Event Handlers 

    protected override void OnPreRenderComplete(EventArgs e) 
    { 
     base.OnPreRenderComplete(e); 
     RegisterListViewButtonsForAsyncPostback(lvInventories, "btnInventoryEntryEdit", "btnInventoryEntryDelete"); 
    } 

    protected override void OnInit(EventArgs e) 
    { 
     base.OnInit(e); 
     this.btnSubmit.Click += new EventHandler(btnSubmit_Click); 
     this.btnInventoryAdd.Click += new EventHandler(btnInventoryAdd_Click); 
     this.lvInventories.ItemCommand += new EventHandler<ListViewCommandEventArgs>(lvInventory_ItemCommand); 
     this.lvInventories.PagePropertiesChanging += new EventHandler<PagePropertiesChangingEventArgs>(lvInventory_PagePropertiesChanging); 
     this.tbInventorySearch.TextChanged += new EventHandler(tbInventorySearch_TextChanged); 
    } 

    void tbInventorySearch_TextChanged(object sender, EventArgs e) 
    { 
     BindInventoryEntries(); 
    } 

    void btnInventoryAdd_Click(object sender, EventArgs e) 
    { 
     InventoryEntrySelected(-1); 
    } 

    void lvInventory_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e) 
    { 
     BindInventoryEntries(); 
    } 

    void lvInventory_ItemCommand(object sender, ListViewCommandEventArgs e) 
    { 
     if (e.CommandName.Equals("edit-item")) 
     { 
      InventoryEntrySelected(Int32.Parse(e.CommandArgument.ToString())); 
     } 
     else if (e.CommandName.Equals("delete-item")) 
     { 
      using (SenProDataDataContext context = new SenProDataDataContext()) 
      { 
       var inv = context.Inventory_Items.SingleOrDefault(x => x.ID == Int32.Parse(e.CommandArgument.ToString())); 
       if (inv != null) 
       { 
        context.Inventory_Items.DeleteOnSubmit(inv); 
        context.SubmitChanges(); 
        BindInventoryEntries(); 
       } 
      } 
     } 
     else if (e.CommandName.Equals("Sort") || e.CommandName.Equals("Page")) { BindInventoryEntries(); } 
    } 

    void btnSubmit_Click(object sender, EventArgs e) 
    { 
     if (!Page.IsValid) { return; } 

     try 
     { 
      using (SenProDataDataContext context = new SenProDataDataContext()) 
      { 
       Inventory_Item inv = null; 
       if (CurrentInventoryID > 0) 
       { 
        inv = context.Inventory_Items.SingleOrDefault(x => x.ID == CurrentInventoryID); 
       } 
       else 
       { 
        inv = new Inventory_Item(); 
        context.Inventory_Items.InsertOnSubmit(inv); 
       } 

       if (inv != null) 
       { 
        if (!String.IsNullOrEmpty(this.tbName.Text)) 
        { 
         inv.Name = this.tbName.Text; 
        } 
        else throw new ApplicationException("Invalid Name"); 

        if (!String.IsNullOrEmpty(this.tbUPC.Text)) 
        { 
         inv.UPC = Convert.ToInt64(this.tbUPC.Text); 
        } 
        else throw new ApplicationException("Invalid UPC#"); 

        if (!String.IsNullOrEmpty(this.tbDescription.Text)) 
        { 
         inv.Description = this.tbDescription.Text; 
        } 
        else throw new ApplicationException("Invalid Description"); 

        if (!String.IsNullOrEmpty(this.tbQuantity.Text)) 
        { 
         inv.Quantity = Convert.ToInt32(this.tbQuantity.Text); 
        } 
        else throw new ApplicationException("Invalid Quantity"); 

        if (!String.IsNullOrEmpty(this.tbPricePerUnit.Text)) 
        { 
         inv.Price = Convert.ToDecimal(this.tbPricePerUnit.Text); 
        } 
        else throw new ApplicationException("Invalid Price"); 

        if (!String.IsNullOrEmpty(this.tbCostPerUnit.Text)) 
        { 
         inv.Cost = Convert.ToDecimal(this.tbCostPerUnit.Text); 
        } 
        else throw new ApplicationException("Invalid Cost"); 

        /** 
        int dep_id = 0; 
        if (Int32.TryParse(this.ddlDepartment.SelectedValue, out loc_id)) 
        { 
         inv.Department = dep_id; 
        } 
        else throw new ApplicationException("Invalid Department"); 

        int category = 0; 
        if (Int32.TryParse(this.ddlCategories.SelectedValue, out category)) 
        { 
         inv.Category = category; 
        } 
        else throw new ApplicationException("Invalid Category"); 
        **/ 
        context.SubmitChanges(); 
        BindInventoryEntries(); 
       } 

      } 
     } 
     catch (ApplicationException ax) 
     { 
      ; 
     } 
    } 

    #endregion 

} 
} 

用戶控制代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

using SenProPOS.Data; 

namespace SenProPOS.Web.Controls 
{ 
public partial class AddEditInventoryItem : System.Web.UI.UserControl 
{ 
    public int? InventoryItemID = -1; 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      BindInventoryItemData(); 
      this.btnSubmit.Click += new ImageClickEventHandler(btnSubmit_Click); 
     } 
    } 

    void btnSubmit_Click(object sender, ImageClickEventArgs e) 
    { 
     try 
     { 
      using (SenProDataDataContext context = new SenProDataDataContext()) 
      { 
       Inventory_Item inv = null; 
       if (InventoryItemID > 0) 
       { 
        inv = context.Inventory_Items.SingleOrDefault(x => x.ID == InventoryItemID); 
       } 
       else 
       { 
        inv = new Inventory_Item(); 
        context.Inventory_Items.InsertOnSubmit(inv); 
       } 

       if (inv != null) 
       { 
        if (!String.IsNullOrEmpty(this.tbName.Text)) 
        { 
         inv.Name = this.tbName.Text; 
        } 
        else throw new ApplicationException("Invalid Name"); 

        if (!String.IsNullOrEmpty(this.tbUPC.Text)) 
        { 
         inv.UPC = Convert.ToInt64(this.tbUPC.Text); 
        } 
        else throw new ApplicationException("Invalid UPC#"); 

        if (!String.IsNullOrEmpty(this.tbDescription.Text)) 
        { 
         inv.Description = this.tbDescription.Text; 
        } 
        else throw new ApplicationException("Invalid Description"); 

        if (!String.IsNullOrEmpty(this.tbQuantity.Text)) 
        { 
         inv.Quantity = Convert.ToInt32(this.tbQuantity.Text); 
        } 
        else throw new ApplicationException("Invalid Quantity"); 

        if (!String.IsNullOrEmpty(this.tbPricePerUnit.Text)) 
        { 
         inv.Price = Convert.ToDecimal(this.tbPricePerUnit.Text); 
        } 
        else throw new ApplicationException("Invalid Price"); 

        if (!String.IsNullOrEmpty(this.tbCostPerUnit.Text)) 
        { 
         inv.Cost = Convert.ToDecimal(this.tbCostPerUnit.Text); 
        } 
        else throw new ApplicationException("Invalid Cost"); 

        /** 
        int dep_id = 0; 
        if (Int32.TryParse(this.ddlDepartment.SelectedValue, out loc_id)) 
        { 
         inv.Department = dep_id; 
        } 
        else throw new ApplicationException("Invalid Department"); 

        int category = 0; 
        if (Int32.TryParse(this.ddlCategories.SelectedValue, out category)) 
        { 
         inv.Category = category; 
        } 
        else throw new ApplicationException("Invalid Category"); 
        **/ 
        context.SubmitChanges(); 
        BindInventoryItemData(); 
       } 

      } 
     } 
     catch (ApplicationException ax) 
     { 
      ; 
     } 
    } 

    public void BindInventoryItemData() 
    { 
     this.tbName.Text = String.Empty; 
     this.tbUPC.Text = String.Empty; 
     this.tbDescription.Text = String.Empty; 
     this.tbQuantity.Text = String.Empty; 
     this.tbPricePerUnit.Text = String.Empty; 
     this.tbCostPerUnit.Text = String.Empty; 
     this.ddlDepartment.SelectedIndex = -1; 
     this.ddlCategory.SelectedIndex = -1; 

     if (InventoryItemID != -1) 
     { 
      using (SenProDataDataContext context = new SenProDataDataContext()) 
      { 
       var inventory = context.Inventory_Items.SingleOrDefault(x => x.ID == InventoryItemID); 
       if (inventory != null) 
       { 
        this.tbName.Text = inventory.Name; 
        this.tbUPC.Text = inventory.UPC.ToString(); 
        this.tbDescription.Text = inventory.Description; 
        this.tbQuantity.Text = inventory.Quantity.ToString(); 
        this.tbPricePerUnit.Text = inventory.Price.ToString(); 
        this.tbCostPerUnit.Text = inventory.Cost.ToString(); 

        /** needs fixing yet 
        var department = this.ddlDepartment.Items.FindByValue(inventory..ToString()); 
        if (department != null) 
        { 
         department.Selected = true; 
        } 

        var category = this.ddlSuppliers.Items.FindByValue(inventory.Category.ToString()); 
        if (supplier != null) 
        { 
         supplier.Selected = true; 
        } 

        **/ 
       } 
       else throw new ApplicationException("The specified item was not found."); 
      } 
     } 
    } 
} 
} 
+1

請,整合的相關部分的代碼進入你的問題,而不是鏈接它!我們希望在本網站上提供完美的問題,永不過期,而且您的鏈接可能會在某一天或另一天失效! –

+0

@AlexanderGalkin對不起,它只是很多代碼縮進,以便它是可讀的。我將編輯該問題。 – yaegerbomb

+0

是的,我訪問了你的鏈接,看到你的代碼很長,所以我寫了「相關部分」。 :)我不確定是否真的需要所有這些「使用」和註釋掉的代碼來理解你的問題。也許你可以跳過更多? –

回答

1

爲什麼你不試試(this.btnSubmit.Click += new ImageClickEventHandler(btnSubmit_Click);)以外不回發?

+0

我只是這樣做了,結果是一個編譯錯誤:'ASP.controls_addeditinventoryitem_ascx'沒有包含'btnSubmit_Click'的定義,也沒有接受'ASP.contro'類型的第一個參數的擴展方法'btnSubmit_Click'可以找到'ls_addeditinventoryitem_ascx'(你是否缺少使用指令或程序集引用?) – yaegerbomb

+1

@yaegerbomb y dont u try put(this.btnSubmit.Click + = new ImageClickEventHandler(btnSubmit_Click);)outside isnot postback – Karthik

+0

OMG i can kiss you馬上。這解決了它。我希望這是錯誤即將結束。我不知道該怎麼感謝你才足夠。 – yaegerbomb

0

只是一個猜測:是對AspImageButton名爲 「btnSubmit按鈕」? Click事件是否正確連接?

+0

據我所知,它連線正確 – yaegerbomb

+1

@yaegerbomb:其中是圖像按鈕的OnClick事件? – Karthik

+0

@karthi 我不太清楚你的意思。和int他後面的代碼(用戶控制)this.btnSubmit.Click + =新的ImageClickEventHandler(btnSubmit_Click);處理onclick?這就是我認爲它被處理的。我知道我可以把一個onclick =「」在imagebutton,但我我不知道我甚至會放什麼。在工作代碼中我​​沒有在.aspx文件中的onclick它也是並且正常工作 – yaegerbomb

0

將控件添加到頁面時,應該使用runat =「server」。我可以舉一個例子

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" 
    CodeFile="Default.aspx.cs" Inherits="_Default" %> 
    <%@ register Src="~/WebUserControl.ascx" TagName="test" TagPrefix="testTag" %> 
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> 
</asp:Content> 
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> 
    <testTag:test id="testId" runat="server" /> 
</asp:Content> 

請看上,

<testTag:test id="testId" runat="server" /> 

而且一定要在你的控制u必須使用類似的東西

<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/btn_submit.png" 
    onclick="ImageButton1_Click" /> 
相關問題