在ASP.net中的GridView中使用TextBox进行自定义控件

时间:2013-08-25 18:47:05

标签: asp.net gridview custom-controls

我已经使用文本框创建了自定义控件。

我在GridView中调用它。触发Update事件时,它始终保存为0。 我使用了以下代码,可以在任何人的帮助下使用。

以下是用于自定义控制的代码。

ASCX代码

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ItemRequiredTextBox.ascx.cs"
Inherits="ItemRequiredTextBox" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<asp:TextBox ID="txtWin_Delievered_Qty" runat="server" > 

<asp:FilteredTextBoxExtender ID="ftbe3" runat="server" TargetControlID="txtWin_Delievered_Qty"
ValidChars="1234567890" />
<asp:CompareValidator ID="CompareValidator1" runat="server" Display="None" ValueToCompare='<%# this.Text2 %>' ControlToValidate="txtWin_Delievered_Qty" Type="Integer"
ErrorMessage="Quantity to be delievered can not be More than Required Quantity"
Operator="LessThanEqual">

<asp:ValidatorCalloutExtender ID="ValidatorCalloutExtender1" TargetControlID="CompareValidator1"
runat="server">

<asp:CompareValidator ID="CompareValidator2" runat="server" Display="None" ValueToCompare='<%# this.Text1 %>' ControlToValidate="txtWin_Delievered_Qty" Type="Integer"
ErrorMessage="Quantity delievered can not be less than delievered Quantity" Operator="GreaterThanEqual">
<asp:ValidatorCalloutExtender ID="ValidatorCalloutExtender2" TargetControlID="CompareValidator2"
runat="server">

ASCX.CS代码

using System;
using System.Data;
using System.ComponentModel;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ItemRequiredTextBox : System.Web.UI.UserControl
{
[Bindable(true, BindingDirection.TwoWay)]

protected void Page_Load(object sender, EventArgs e)
{

}

private int TexT1;
private int TexT2;

public int Text1
{
get { return TexT1; }
set { TexT1 = value; }
}

public int Text2
{
get { return TexT2; }
set { TexT2 = value; }
}
public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? String.Empty : s);
}

set
{
ViewState["Text"] = value;
}
}




}

1 个答案:

答案 0 :(得分:0)

在usercontrol中创建一个公共属性以获取文本框值:

public string Text
{
    get
    {
        return txtWin_Delievered_Qty.Text;
    }
}

并访问gridview update事件中的值:

ItemRequiredTextBox irtBox = (ItemRequiredTextBox)gvMyGridView.Rows[e.Item.ItemIndex].FindControl("updatedBy");
string myText = irtBox.Text;

这里gvMyGridView是你的gridview