Visual Studio用户控件ASP,C#,代码隐藏问题
好吧,这可能是在黑暗中拍摄的,因为很难猜到这里发生了什么。但我已经没有选择了 因此,我有一个页面的代码,用户可以填写页面上的输入字段,然后单击submit按钮,将信息添加到数据库中。这100%有效。但是,我想把这个功能放在一个用户控件中,这样我就可以在不同的页面上使用它,但效果相同。但是我不能让它工作 这就是工作原理 btnSubmit_Click方法会将信息毫无问题地添加到数据库中 现在是用户。这没什么用。我所做的唯一区别是它使用了一个ASPImageButton,我原来只是一个普通的ASPButton,但这并没有造成什么不同 就像我说的,在黑暗中开枪。如果有人对如何尝试或修复有任何建议,请让我知道。如果需要,我可以提供更多信息 工作代码:Visual Studio用户控件ASP,C#,代码隐藏问题,c#,asp.net,webusercontrol,asp.net-controls,C#,Asp.net,Webusercontrol,Asp.net Controls,好吧,这可能是在黑暗中拍摄的,因为很难猜到这里发生了什么。但我已经没有选择了 因此,我有一个页面的代码,用户可以填写页面上的输入字段,然后单击submit按钮,将信息添加到数据库中。这100%有效。但是,我想把这个功能放在一个用户控件中,这样我就可以在不同的页面上使用它,但效果相同。但是我不能让它工作 这就是工作原理 btnSubmit_Click方法会将信息毫无问题地添加到数据库中 现在是用户。这没什么用。我所做的唯一区别是它使用了一个ASPImageButton,我原来只是一个普通的ASPB
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
}
}
猜测一下:AspImageButton是否命名为“btnSubmit”?单击事件是否正确连接?为什么不尝试将
(this.btnSubmit.Click+=新建图像ClickEventHandler(btnSubmit\u Click);)
外部未回发?将控件添加到页面时,应使用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" />
确保在你的控制下你用过类似的东西
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/btn_submit.png"
onclick="ImageButton1_Click" />
请将代码的相关部分整合到您的问题中,而不是将其链接!我们想在这个网站上有完整的形式,永远不会过期,你的链接可以过期一天或另一天伟大的问题@AlexanderGalkin抱歉,为了便于阅读,需要缩进很多代码。我会编辑这个问题。是的,我访问了你的链接,看到你的代码很长,所以我写了“相关部分”。)我不确定是否真的需要所有这些“使用”和注释掉的代码来理解您的问题。也许你可以跳过更多?你在调试器中跳过了吗?@Prescott我一直在尝试。我一直在使用VS网站上的帮助页面,但是调试器说不会命中断点,因为符号没有加载到文档中。我不知道这意味着什么,所以我不知道我需要做什么来修复它。我最好的猜测是,它不认为用户控件已加载?但这是我在网页上看到的。据我所知,它接线正确。@yaegerbomb:image按钮的OnClick事件在哪里?@karthi我不太清楚你的意思。我用id和int链接了按钮(用户控件)this.btnSubmit.Click+=新建ImageClickEventHandler(btnSubmit\u Click);处理onclick?这就是我认为它是如何处理的。我知道我可以在imagebutton中添加onclick=“”,但我甚至不确定我会添加什么。在工作代码中,.aspx文件中没有onclick,也可以正常工作。我只是这样做了,结果是编译错误:“ASP.controls\u addeditinventoryitem\u ascx”不包含“btnSubmit\u Click”的定义,并且没有扩展方法“btnSubmit\u Click”接受类型的第一个参数“ASP.controls_addeditinventoryitem_ascx”无法找到(您是否缺少using指令或程序集引用?@yaegerbomb,请不要尝试将(this.btnSubmit.Click+=new ImageClickEventHandler(btnSubmit_Click);)放在不是postbackOMG的外部。我现在可以吻您了。这就解决了问题。我希望这就是我所犯错误的结束。我非常感谢你。如果你把它作为一个答案,我会把它标记为正确的,这样你就可以得到它的信用。
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/btn_submit.png"
onclick="ImageButton1_Click" />