Telerik RadGrid - Not giving new value in the UpdateCommand event handler - c#

The problem i am facing is in this line.
decimal amount = Convert.ToDecimal(Amt.Text);
Amt.Text is not giving me the new entered value when i change it and try to save it. But it is giving me the old value. I don't know why ? Please suggest the fix.
I have a search button also in the webpage which is needed to rebind the Grid with the latest data. I think when i click the save link and gvLineItems_UpdateCommand is called , the rebinding is happening before that on the RADGRID (internally).
ASPX Code:-
<asp:content id="Content2" contentplaceholderid="MainContent" runat="server">
<script language="javascript">
function captureVal(sender, args){
alert('hi');
}
</script>
<table>
<tr>
<td>
<asp:Label ID="lblPartnerID" runat="server" Text="PartnerID" Font-Bold="true"></asp:Label>
</td>
<td>
<telerik:RadTextBox ID="tbPartnerID" runat="server" MaxLength="35" Width="243px" />
</td>
<td>
<asp:Button ID="btnSearch" runat="server" Width="80px" Visible="true" Text="Search" />
</td>
</tr>
<tr>
<td style="text-align:right">
<asp:Button ID="btnSave" runat="server" Visible="true" Text="Save" />
</td>
<td style="text-align:left">
<asp:Button ID="btnCancel" runat="server" Visible="true" Text="Cancel" />
</td>
</tr>
<tr>
<td>
Partner SSN/TIN:
</td>
<td>
<telerik:RadTextBox ID="tbPartnerEIN" runat="server" MaxLength="9" TabIndex="5">
</telerik:RadTextBox>
<label id="Label1" runat="Server" style="color: Navy">* No Dashes</label>
</td>
<td style="width:10px"></td>
<td style="text-align:left">
Address Line 1:
</td>
<td>
<telerik:RadTextBox ID="tbAddressLine1" runat="server" TabIndex="10" />
<label id="label2" runat="server" style="color: navy">
* No dashes</label>
</td>
</tr>
<tr>
<td>
Custodial EIN:
</td>
<td>
<telerik:RadTextBox ID="tbCustodialEIN" runat="server" MaxLength="9" TabIndex="10">
</telerik:RadTextBox>
<label id="Label3" runat="Server" style="color: Navy">* No Dashes</label>
</td>
<td style="width:10px"></td>
<td style="text-align:left">
Address Line 2:
</td>
<td>
<telerik:RadTextBox ID="tbAddressLine2" runat="server" TabIndex="10" />
<label id="Label4" runat="Server" style="color: Navy">
* No Dashes</label>
</td>
</tr>
<tr>
<td>
Format SSN/TIN:
</td>
<td>
<telerik:RadComboBox ID="cboFormatSSNTIN" Height="200px" runat="server" Width="246px"
TabIndex="41">
<Items>
<telerik:RadComboBoxItem Text="" Value="" />
<telerik:RadComboBoxItem Text="XXX-XX-XXXX" Value="0" />
<telerik:RadComboBoxItem Text="XX-XXXXXXX" Value="1" />
</Items>
</telerik:RadComboBox>
</td>
<td style="width:10px"></td>
<td style="text-align:left">
City:
</td>
<td>
<telerik:RadTextBox ID="tbCity" runat="server" MaxLength="22" Width="243px" TabIndex="45" />
</td>
</tr>
<tr>
<td>
Partner Name 1:
</td>
<td>
<telerik:RadTextBox ID="tbPartnerName1" runat="server" MaxLength="35" Width="243px"
TabIndex="15" />
</td>
<td style="width:10px"></td>
<td style="text-align:left">
State:
</td>
<td>
<telerik:RadComboBox ID="ddlState" runat="server" AllowCustomText="true" Filter="Contains"
Width="243px" TabIndex="35" Height="100" />
</td>
</tr>
<tr>
<td>
Partner Name 2:
</td>
<td>
<telerik:RadTextBox ID="tbPartnerName2" runat="server" MaxLength="35" Width="243px"
TabIndex="20" />
</td>
<td style="width:10px"></td>
<td style="text-align:left">
Zip:
</td>
<td>
<telerik:RadTextBox ID="tbZipcode" runat="server" MaxLength="12" Width="243px" TabIndex="55" />
</td>
</tr>
<tr>
<td>
Entity Type:
</td>
<td>
<telerik:RadComboBox ID="lbEntityType" Height="150px" runat="server" Width="246px"
TabIndex="40" />
<asp:RequiredFieldValidator ID="Requiredfieldvalidator1" runat="server" ErrorMessage="Select any one Entity type"
ControlToValidate="lbEntityType" InitialValue="0" Display="None">*</asp:RequiredFieldValidator>
</td>
<td style="width:10px"></td>
<td style="text-align:left">
Foreign Address:
</td>
<td>
<telerik:RadTextBox ID="tbForeignAddress" runat="server" Width="243px" TabIndex="35" />
</td>
</tr>
<tr>
<td colspan="2">
</td>
<td style="width:10px"></td>
<td style="text-align:left">
Domestic or Foreign:
</td>
<td>
<telerik:RadComboBox ID="lbCountry" runat="server" Width="246px" TabIndex="35" />
</td>
</tr>
<tr>
<td>
<asp:RadioButtonList ID="rbFinalAmend" runat="server" RepeatDirection="Horizontal">
<asp:ListItem>Final K-1</asp:ListItem>
<asp:ListItem>Amended K-1</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td colspan="2">
<asp:RadioButtonList ID="rbGenLtd" runat="server" RepeatDirection="Horizontal">
<asp:ListItem>General partner or LLC member-manager</asp:ListItem>
<asp:ListItem>Limited partner or other LLC member</asp:ListItem>
</asp:RadioButtonList>
</td>
<td style="width:10px"></td>
<td>
Nonrecourse
</td>
<td>
$<telerik:RadNumericTextBox ID="txtNonrecourse" DataType="System.Double" NumberFormat-DecimalDigits="2"
Width="200px" runat="server" />
</td>
</tr>
<tr>
<td colspan="2">
<br />
<asp:CheckBox ID="cbTax" Text="Tax basis" runat="server" />
<asp:CheckBox ID="cbGaap" Text="GAAP" runat="server" />
<asp:CheckBox ID="cbSection" Text="Section 704(b) book" runat="server" />
</td>
<td style="width:10px"></td>
<td>
Qualified nonrecourse financing
</td>
<td>
$<telerik:RadNumericTextBox ID="txtQualified" DataType="System.Double" NumberFormat-DecimalDigits="2"
Width="200px" runat="server" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:CheckBox ID="cbOther" Text="Other(explain)" runat="server" />
</td>
<td style="width:10px"></td>
<td>
Recourse
</td>
<td>
$<telerik:RadNumericTextBox ID="txtRecourse" DataType="System.Double" NumberFormat-DecimalDigits="2"
Width="200px" runat="server" />
</td>
</tr>
</table>
<table>
<tr>
<td colspan="3">
<br />
<span style="font-weight: bold; font-size: 10pt">(M) Did the partner contribute property
with a built-in gain or loss?</span>
<br />
<hr size="1" />
</td>
</tr>
<tr>
<td>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<asp:RadioButtonList ID="rbContributedProperty" runat="server" RepeatDirection="Horizontal">
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:RadioButtonList>
<br />
</td>
</tr>
</table>
</td>
</tr>
</table>
<table>
<tr>
<td>
<table>
<tr>
<td>
<table>
<thead colspan="2">
<span style="font-weight: bold; font-size: 10pt">(L) Partner's capital account analysis</span>
</thead>
<hr size="1" />
<br />
<br />
<tr>
<td>
Beginning capital account
</td>
<td>
$<telerik:RadNumericTextBox ID="txtCapBeginning" DataType="System.Double" NumberFormat-DecimalDigits="2"
Width="200px" runat="server">
</telerik:RadNumericTextBox>
</td>
</tr>
<tr>
<td>
Capital contributed during the year
</td>
<td>
$<telerik:RadNumericTextBox ID="txtCapContributed" DataType="System.Double" NumberFormat-DecimalDigits="2"
Width="200px" runat="server">
</telerik:RadNumericTextBox>
</td>
</tr>
<tr>
<td>
Current year increase(decrease)
</td>
<td>
$<telerik:RadNumericTextBox ID="txtIncrease" DataType="System.Double" NumberFormat-DecimalDigits="2"
Width="200px" runat="server">
</telerik:RadNumericTextBox>
</td>
</tr>
<tr>
<td>
Withdrawals & distributions <span style="color: Red">(-)</span>
</td>
<td>
$<telerik:RadNumericTextBox ID="txtWithdrawals" DataType="System.Double" NumberFormat-DecimalDigits="2"
Width="200px" ForeColor="Red" runat="server">
</telerik:RadNumericTextBox>
</td>
</tr>
<tr>
<td>
Ending capital account
</td>
<td>
$<telerik:RadNumericTextBox ID="txtCapEnding" DataType="System.Double" NumberFormat-DecimalDigits="2"
Width="200px" runat="server" ReadOnly="true" />
</td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td>
</td>
<td align="center">
<b> Beginning</b>
</td>
<td align="center">
<b>Ending</b>
</td>
</tr>
<tr>
<td>
Profit
</td>
<td>
<telerik:RadNumericTextBox ID="txtBeginningProfit" DataType="System.Double"
NumberFormat-DecimalDigits="6" Width="200px" runat="server" />%
</td>
<td>
<telerik:RadNumericTextBox ID="txtEndProfit" DataType="System.Double" NumberFormat-DecimalDigits="6"
Width="200px" runat="server" />%
</td>
</tr>
<tr>
<td>
Loss
</td>
<td>
<telerik:RadNumericTextBox ID="txtBeginningLoss" DataType="System.Double"
NumberFormat-DecimalDigits="6" Width="200px" runat="server" />%
</td>
<td>
<telerik:RadNumericTextBox ID="txtEndLoss" DataType="System.Double" NumberFormat-DecimalDigits="6"
Width="200px" runat="server" />%
</td>
</tr>
<tr>
<td>
Capital
</td>
<td>
<telerik:RadNumericTextBox ID="txtBeginningCap" DataType="System.Double"
NumberFormat-DecimalDigits="6" Width="200px" runat="server" />%
</td>
<td>
<telerik:RadNumericTextBox ID="txtEndCap" DataType="System.Double" NumberFormat-DecimalDigits="6"
Width="200px" runat="server" />%
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<telerik:RadGrid runat="server" ID="gvLineItems" AllowMultiRowSelection="true" AllowMultiRowEdit="False"
AllowAutomaticUpdates="false" AllowAutomaticInserts="false" Width="100%" AllowAutomaticDeletes="False" GridLines="None" Pagesize="10"
AllowPaging="True" AutoGenerateColumns="False" PagerStyle-Mode="NextPrevAndNumeric" OnNeedDataSource="gvLineItems_NeedDataSource">
<MasterTableView InsertItemDisplay="Bottom" AutoGenerateColumns="false" AllowAutomaticUpdates="false" EditMode="InPlace"
DataKeyNames="SchK1ID,LineNumber,LineCode,Amount"
AllowAutomaticDeletes="false" Width="100%" AllowAutomaticInserts="false" CommandItemDisplay="None">
<HeaderStyle HorizontalAlign="Left" Wrap="false" />
<ItemStyle HorizontalAlign="Left"/>
<Columns>
<telerik:GridBoundColumn HeaderText="Line Number" DataField="LineNumber" SortExpression="LineNumber" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Top" ReadOnly="true" />
<telerik:GridBoundColumn HeaderText="Line Code" DataField="LineCode" SortExpression="LineCode" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Top" ReadOnly="true" />
<telerik:GridBoundColumn HeaderText="Description" DataField="LineDescription" SortExpression="LineDescription" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Top" ReadOnly="true" />
<telerik:GridTemplateColumn DataType="System.Decimal" HeaderText="Amount" DataField="Amount" SortExpression="Amount" UniqueName="Amount" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Right" ItemStyle-VerticalAlign="Top">
<ItemTemplate><%#Eval("Amount", "{0:c}")%></ItemTemplate>
<EditItemTemplate>
<telerik:RadNumericTextBox runat="server" id="tbAmount" width="75px" EmptyMessage="0" DataType="System.Decimal" Text='<%#Eval("Amount")%>' NumberFormat-DecimalDigits="3">
</telerik:RadNumericTextBox>
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridEditCommandColumn UniqueName="Save" ButtonType="LinkButton" EditText="Edit" InsertText="Insert" UpdateText="Save"/>
</Columns>
</MasterTableView>
<ClientSettings>
<Selecting AllowRowSelect="True"/>
</ClientSettings>
</telerik:RadGrid>
</td>
</tr>
</table>
C# Code Behind:
public partial class EFileK1ImageEdit : System.Web.UI.Page
{
private string m_EFSTaxPayerID;
public int PartnerId {
get {
if (!string.Empty.Equals(tbPartnerID.Text.Trim())) {
return Convert.ToInt32(tbPartnerID.Text.Trim());
}
return 0;
}
}
protected void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack) {
this.LoadPartner();
this.PopulateCapitalAccount();
}
}
public void LoadPartner()
{
if (!(PartnerId == 0)) {
Partner p = Partner.GetEfilePartnerByID(PartnerId, ProfileExtensions.GetCurrentYear(), ProfileExtensions.GetCurrentClient(), ProfileExtensions.GetCurrentUsername());
if ((p != null)) {
PopulateInfo(p, ProfileExtensions.GetCurrentYear());
}
}
}
public void PopulateDropDowns()
{
this.lbEntityType.DataSource = IDMS.BusinessObjects.EntityCode.GetEntityType(ProfileExtensions.GetCurrentClient(), ProfileExtensions.GetCurrentUsername());
this.lbEntityType.DataTextField = "Explanation";
this.lbEntityType.DataValueField = "EntityCodeID";
this.lbEntityType.DataBind();
this.ddlState.DataSource = IDMS.BusinessObjects.SystemSettings.GetStateCodes(ProfileExtensions.GetCurrentClient(), ProfileExtensions.GetCurrentUsername());
this.ddlState.DataTextField = "StateCode";
this.ddlState.DataValueField = "StateCode";
this.ddlState.DataBind();
this.ddlState.Items.FindItemByText(". ").Text = "FR";
this.lbCountry.DataSource = IDMS.BusinessObjects.MailCode.GetAllMailCodes(ProfileExtensions.GetCurrentClient(), ProfileExtensions.GetCurrentUsername());
this.lbCountry.DataTextField = "Explanation";
this.lbCountry.DataValueField = "MailCodeID";
this.lbCountry.DataBind();
}
private void SetStateControl(string value)
{
RadComboBoxItem item = this.ddlState.FindItemByValue(value);
if (item == null) {
this.ddlState.Text = value;
} else {
item.Selected = true;
}
if (value.Trim() == ".") {
this.ddlState.Text = "";
}
}
protected void PopulateInfo(Partner p, int taxYear)
{
PopulateDropDowns();
this.tbPartnerID.Text = this.PartnerId.ToString();
}
private void PopulateCapitalAccount()
{
IDMS.BusinessObjects.CapitalAccount ca = IDMS.BusinessObjects.CapitalAccount.GetEFileCapitalAccount(this.PartnerId, ProfileExtensions.GetCurrentYear(), ProfileExtensions.GetCurrentClient(), ProfileExtensions.GetCurrentUsername());
// SetManualAdjustmentMessage(ca)
if (ca != null) {
var _with1 = ca;
this.rbFinalAmend.Items[0].Selected = _with1.FinalK1;
this.rbFinalAmend.Items[1].Selected = _with1.AmendedK1;
this.rbGenLtd.Items[0].Selected = _with1.GeneralPartner;
this.rbGenLtd.Items[1].Selected = _with1.LimitedPartner;
this.txtBeginningCap.Value = _with1.BeginningCapitalPercent * 100;
this.txtBeginningLoss.Value = _with1.BeginningLossPercent * 100;
this.txtBeginningProfit.Value = _with1.BeginningProfitPercent * 100;
this.txtEndCap.Value = _with1.EndingCapitalPercent * 100;
this.txtEndLoss.Value = _with1.EndingLossPercent * 100;
this.txtEndProfit.Value = _with1.EndingProfitPercent * 100;
this.txtNonrecourse.Value = _with1.LiabilityShareNonRecourse;
this.txtQualified.Value = _with1.LiabilitySharedQualifiedNonRecourse;
this.txtRecourse.Value = _with1.LiabilitySharedRecourse;
} else {
this.rbContributedProperty.Items[0].Selected = false;
this.rbContributedProperty.Items[1].Selected = false;
}
}
protected void gvLineItems_UpdateCommand(object sender, GridCommandEventArgs eventData)
{
//Validate Parameters
if (eventData.Item is GridEditableItem && eventData.Item.IsInEditMode) {
DataTable dt = IDMS.BusinessObjects.EfileMapping.GetEFileLineItemsByPartner(ProfileExtensions.GetCurrentClient(), ProfileExtensions.GetCurrentUsername(), ProfileExtensions.GetCurrentYear(), PartnerId);
//Declare Variables
GridEditableItem editableItem = eventData.Item as GridEditableItem;
int LineNumber = Convert.ToInt32(editableItem.GetDataKeyValue("LineNumber"));
string LineCode = Convert.ToString(editableItem.GetDataKeyValue("LineCode"));
decimal oldAmount = Convert.ToDecimal(dt.Select(string.Format("LineNumber={0} and LineCode='{1}'", LineNumber, LineCode))[0]["Amount"]);
RadNumericTextBox Amt = editableItem.FindControl("tbAmount") as RadNumericTextBox;
decimal amount = Convert.ToDecimal(Amt.Text);
decimal diff = amount - oldAmount;
txtIncrease.Text = (Convert.ToDecimal(txtIncrease.Text) + diff).ToString();
txtCapEnding.Text = (Convert.ToDecimal(txtCapEnding.Text) + diff).ToString();
saveCapitalAccount();
IDMS.BusinessObjects.EfileMapping.UpdateEFileLineItemsByPartner(ProfileExtensions.GetCurrentClient(), ProfileExtensions.GetCurrentUsername(), ProfileExtensions.GetCurrentYear(), PartnerId, amount, LineNumber, LineCode);
}
}
protected void gvLineItems_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
gvLineItems.DataSource = IDMS.BusinessObjects.EfileMapping.GetEFileLineItemsByPartner(ProfileExtensions.GetCurrentClient(), ProfileExtensions.GetCurrentUsername(), ProfileExtensions.GetCurrentYear(), PartnerId);
}
protected void btnSearch_Click(object sender, EventArgs e)
{
this.LoadPartner();
this.PopulateCapitalAccount();
gvLineItems.Rebind();
}
private void btnSave_Click(object sender, System.EventArgs e)
{
IDMS.BusinessObjects.Partner p = new IDMS.BusinessObjects.Partner();
var _with2 = p;
_with2.PartnerID = int.Parse(tbPartnerID.Text);
_with2.EFSNameLine1 = tbPartnerName1.Text;
UpdateEFileDetailPartnerGridByYear(ProfileExtensions.GetCurrentClient(), ProfileExtensions.GetCurrentYear(), this.PartnerId);
}
public void saveCapitalAccount()
{
IDMS.BusinessObjects.CapitalAccount ca = new IDMS.BusinessObjects.CapitalAccount();
var _with3 = ca;
_with3.PartnerID = this.PartnerId;
_with3.CalendarYear = ProfileExtensions.GetCurrentYear();
_with3.FinalK1 = this.rbFinalAmend.Items[0].Selected;
_with3.AmendedK1 = this.rbFinalAmend.Items[1].Selected;
_with3.GeneralPartner = this.rbGenLtd.Items[0].Selected;
_with3.LimitedPartner = this.rbGenLtd.Items[1].Selected;
_with3.BeginningCapitalPercent = this.txtBeginningCap.Value.GetValueOrDefault(0) / 100;
_with3.BeginningLossPercent = this.txtBeginningLoss.Value.GetValueOrDefault(0) / 100;
_with3.BeginningProfitPercent = this.txtBeginningProfit.Value.GetValueOrDefault(0) / 100;
_with3.EndingCapitalPercent = this.txtEndCap.Value.GetValueOrDefault(0) / 100;
_with3.EndingLossPercent = this.txtEndLoss.Value.GetValueOrDefault(0) / 100;
_with3.EndingProfitPercent = this.txtEndProfit.Value.GetValueOrDefault(0) / 100;
_with3.LiabilityShareNonRecourse = this.txtNonrecourse.Value.GetValueOrDefault(0);
_with3.LiabilitySharedQualifiedNonRecourse = this.txtQualified.Value.GetValueOrDefault(0);
_with3.LiabilitySharedRecourse = this.txtRecourse.Value.GetValueOrDefault(0);
_with3.AccountAnalysisBeginningCapital = this.txtCapBeginning.Value.GetValueOrDefault(0);
_with3.AccountAnalysisContributions = this.txtCapContributed.Value.GetValueOrDefault(0);
_with3.AccountAnalysisIncreaseDecrease = this.txtIncrease.Value.GetValueOrDefault(0);
_with3.AccountAnalysisWithdrawalsDistributions = this.txtWithdrawals.Value.GetValueOrDefault(0);
_with3.AccountAnalysisEndingCapital = _with3.AccountAnalysisBeginningCapital + _with3.AccountAnalysisContributions + _with3.AccountAnalysisIncreaseDecrease - _with3.AccountAnalysisWithdrawalsDistributions;
_with3.AccountAnalysisGAAP = this.cbGaap.Checked;
_with3.AccountAnalysisOther = this.cbOther.Checked;
_with3.AccountAnalysisSection704b = this.cbSection.Checked;
_with3.AccountAnalysisTaxBasis = this.cbTax.Checked;
_with3.ContributePropertyYes = this.rbContributedProperty.Items[0].Selected;
_with3.ContributePropertyNo = this.rbContributedProperty.Items[1].Selected;
_with3.SaveEfile(ProfileExtensions.GetCurrentClient(), ProfileExtensions.GetCurrentUsername());
}
public EFileK1ImageEdit()
{
Load += Page_Load;
Init +=EFileK1ImageEdit_Init;
}
private void EFileK1ImageEdit_Init(object sender, EventArgs e)
{
btnSave.Click+=btnSave_Click;
btnCancel.Click +=btnCancel_Click;
btnSearch.Click+=btnSearch_Click;
gvLineItems.NeedDataSource +=gvLineItems_NeedDataSource;
gvLineItems.UpdateCommand +=gvLineItems_UpdateCommand;
}
}

Related

C# = The ListView raised event which wasn't handled.

There is a ListView which shows the data that were retrieved from the database.
I want to edit a single record on a ListView by clicking the edit button beside it but if I press the edit button, I get the error:
The ListView 'lvItemView' raised event ItemEditing which wasn't handled.
Here is the ListView:
<asp:ListView ID="lvItemView" runat="server" ItemPlaceholderID="itemHolder">
<LayoutTemplate>
<table style="color: Black;" width="100%" border="0" cellpadding="5">
<tr>
<th style="text-align: center;">Customer ID</th>
<th style="text-align: center;">Contact Name</th>
<th style="text-align: center;">Company</th>
<th style="text-align: center;">Created By</th>
<th style="text-align: center;">Created Date</th>
<th style="text-align: center;">Modified By</th>
<th style="text-align: center;">Modified Date</th>
</tr>
<asp:PlaceHolder ID="itemHolder" runat="server"></asp:PlaceHolder>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td align="left">
<asp:Label ID="lblCustomerID" runat="Server" Text='<%#Eval("_CustomerID") %>' />
</td>
<td align="left">
<asp:Label ID="lblContactName" runat="Server" Text='<%#Eval("_ContactName") %>' />
</td>
<td align="left">
<asp:Label ID="lblCompany" runat="Server" Text='<%#Eval("_CompanyID") %>' />
</td>
<td align="left">
<asp:Label ID="lblCreatedBy" runat="Server" Text='<%#Eval("_CreatedBy") %>' />
</td>
<td align="left">
<asp:Label ID="lblCreatedDate" runat="Server" Text='<%#Eval("_CreatedDate") %>' />
</td>
<td align="left">
<asp:Label ID="lblModifiedBy" runat="Server" Text='<%#Eval("_ModifiedBy") %>' />
</td>
<td align="left">
<asp:Label ID="lblModifiedDate" runat="Server" Text='<%#Eval("_ModifiedDate") %>' />
</td>
<td align="left">
<asp:LinkButton ID="EditButton" runat="Server" Text="Edit" CommandName="Edit" CommandArgument='<%#DataBinder.Eval(Container, "DataItemIndex")%>' />
</td>
<td align="left">
<asp:LinkButton ID="Delete" runat="Server" Text="Delete" CommandName="Delete" CommandArgument='<%#DataBinder.Eval(Container, "DataItemIndex")%>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
And the code behind
protected void lvItemView_ItemEditing(object sender, ListViewEditEventArgs e)
{
System.Web.UI.WebControls.Label index_id = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblCustomerID");
int customerID = int.Parse(index_id.Text);
Item item = new Item();
item._CustomerID = customerID;
System.Web.UI.WebControls.Label cmp_id = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblCompany");
int companyID = int.Parse(cmp_id.Text);
item._CompanyID = companyID;
System.Web.UI.WebControls.Label samp = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblContactName");
item._ContactName = samp.Text;
samp = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblCreatedBy");
item._CreatedBy = samp.Text;
samp = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblCreatedDate");
item._CreatedDate = samp.Text;
samp = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblModifiedBy");
item._ModifiedBy = samp.Text;
samp = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblModifiedDate");
item._ModifiedDate = samp.Text;
CustomerID = item._CustomerID.ToString();
ContactName = item._ContactName.ToString();
CompanyID = item._CompanyID.ToString();
CreatedBy = item._CreatedBy.ToString();
CreatedDate = item._CreatedDate.ToString();
ModifiedBy = item._ModifiedBy.ToString();
ModifiedDate = item._ModifiedDate.ToString();
modAdd.Show();
}
I am new to asp.net and c# so I have no idea what to do with this kind of error.
set the event lvItemView_ItemEditing in OnItemEditing which will be triggered when click on edit button
<asp:ListView ID="lvItemView" runat="server" ItemPlaceholderID="itemHolder" OnItemEditing="lvItemView_ItemEditing">
and also set data source if you are binding on pageload
lvItemView.DataSource = SomeData;
lvItemView.DataBind();

How to control button from panel

I am new to ASP.NET. I have taken a Panel. Inside the Panel, I have taken a table. Inside the table there are TextBox, RadioButton and a Button controls.
I have written some code in button_click event.
But nothing happens. button_click event is not firing.
What to do now ?
I have also some other things in panel. But no click event is working.
Code is below:
<asp:Panel ID="Pnlproducts" runat="server" ScrollBars="Auto" Height="500px" BorderColor="Black" BorderStyle="Inset" BorderWidth="1px">
<asp:DataList ID="DLProducts" runat="server" RepeatColumns="3" Width="600px" OnSelectedIndexChanged="DLProducts_SelectedIndexChanged">
<ItemTemplate >
<table align="left" class="auto-style6" style="border-color:#9900FF;border-left:1px;border-right:1px;border-top:1px;">
<tr>
<td style="border-color: #000000; border-width: 1px; border-bottom-style: ridge" class="auto-style8">
<asp:Label ID="lblProductName" runat="server" Text="<%# Bind('ProductName') %>" Font-Bold="True"></asp:Label>
</td>
</tr>
<tr>
<td class="auto-style9">
<asp:Image ID="Image2" runat="server" Height="160px" ImageUrl="<%# Bind('ProductImage') %>" Width="173px" Style="border:ridge 1px black" />
</td>
</tr>
<tr>
<td class="auto-style9"><strong>Price:</strong><asp:Label ID="lblPrice" runat="server" Text="<%# Bind('ProductPrice') %>" Font-Bold="True" ForeColor="Red"></asp:Label>
</td>
</tr>
<tr>
<td class="auto-style9">
<asp:Button ID="Addtocartbtn" runat="server" BorderStyle="Solid" BorderWidth="1px" OnClick="Addtocartbtn_Click" Text="Add to Cart" Width="170px" BorderColor="Black" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</asp:Panel>
<asp:Panel ID="Panel111" runat="server" ScrollBars="Auto" Height="500px" BorderColor="Black" BorderStyle="Inset" BorderWidth="1px">
<table class="auto-style16">
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</asp:Panel>
</td>
<td>
<asp:Panel ID="pnlCatagory" runat="server" Height="500px" ScrollBars="Auto" BorderColor="Black" BorderStyle="Inset" BorderWidth="1px">
<div class="auto-style7">
<asp:DataList ID="dlCatagory" runat="server" BackColor="White" BorderColor="#CCCCCC" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal">
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
<ItemTemplate>
<asp:LinkButton ID="LBTNCatagoty" runat="server" CommandArgument="<%# Bind('CatagoryID') %>" Font-Bold="True" OnClick="LBTNCatagoty_Click" Text="<%# Bind('CatagoryName') %>"></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</div>
</asp:Panel>
<asp:Panel ID="Panel222" runat="server" Height="500px" ScrollBars="Auto" BorderColor="Black" BorderStyle="Inset" BorderWidth="1px">
<table class="auto-style10">
<tr>
<td class="auto-style7" colspan="2"><strong>Please Type Your Details</strong></td>
</tr>
<tr>
<td class="auto-style12"></td>
<td class="auto-style11"></td>
</tr>
<tr>
<td class="auto-style15">Name :</td>
<td class="johnykbd8">
<asp:TextBox ID="TextBoxUserName" runat="server" Width="180px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style15">E-mail :</td>
<td class="johnykbd8">
<asp:TextBox ID="TextBoxEmail" runat="server" Width="180px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style15">Address:</td>
<td class="johnykbd8">
<asp:TextBox ID="TextBoxAdress" runat="server" Height="90px" TextMode="MultiLine" Width="180px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style15">Gender :</td>
<td>
<asp:RadioButton ID="RadioButton1" runat="server" GroupName="GenderGroup" Text="Male" /><br />
<asp:RadioButton ID="RadioButton2" runat="server" GroupName="GenderGroup" Text="Female" />
</td>
</tr>
<tr>
<td class="auto-style15">Mobile:</td>
<td class="johnykbd8">
<asp:TextBox ID="TextBoxMobileNum" runat="server" Width="180px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style15">Photo :</td>
<td class="johnykbd8">
<asp:FileUpload ID="FileUploaduserphoto" runat="server" Width="180px" />
</td>
</tr>
<tr>
<td class="auto-style15">Payment :</td>
<td>
<asp:RadioButton ID="RadioButton3" runat="server" GroupName="Paymentgroup" Text="Visa" /><br />
<asp:RadioButton ID="RadioButton4" runat="server" GroupName="Paymentgroup" Text="MasterCard" /><br />
<asp:RadioButton ID="RadioButton5" runat="server" GroupName="Paymentgroup" Text="Cash On Delivery" />
</td>
</tr>
<tr>
<td class="auto-style15">Review :</td>
<td>
<asp:TextBox ID="TextBoxreview" runat="server" Width="180px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style15">Total : </td>
<td>BDT :<asp:Label ID="Labelshopamount" runat="server"></asp:Label></td>
</tr>
<tr>
<td colspan="2">
<asp:ImageButton ID="ImageButtonOrderNow" runat="server" ImageUrl="~/Images/Designing/order.jpg" OnClick="ImageButtonOrderNow_Click" Width="250px" />
</td>
</tr>
</table>
</asp:Panel>
If the button is in a datalist or gridview for example, set the CommandName property. The databound object (datalist, gridview) has ItemCommand-event. Put the code in this event.
Pseudo code:
if (e.CommandName.equals(your_command_name)){ label1.text = e.CommandArgument };

FindControl returns empty textbox object / null

I have a html table on a page:
<table id="tblMain" runat="server" style="margin-left: auto; margin-right: auto;">
<tr>
<td>
Safety
</td>
<td>
<asp:TextBox ID="txtSafety" Width="400px" ReadOnly="true" TextMode="MultiLine" runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnSafety" runat="server" Width="150px" Text="Edit"
OnClick="Edit_Text" CommandArgument="0" />
</td>
</tr>
<tr>
<td>
Environment
</td>
<td>
<asp:TextBox ID="txtEnvironment" Width="400px" ReadOnly="true" TextMode="MultiLine" runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="bntEnvironment" Width="150px" runat="server" Text="Edit"
OnClick="Edit_Text" CommandArgument="1" />
</td>
</tr>
<tr>
<td>
Quality
</td>
<td>
<asp:TextBox ID="txtQuality" Width="400px" ReadOnly="true" TextMode="MultiLine" runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnQuality" Width="150px" runat="server" Text="Edit"
OnClick="Edit_Text" CommandArgument="2" />
</td>
</tr>
<tr>
<td>
Ferrous System
</td>
<td>
<asp:TextBox ID="txtFerrousSystem" Width="400px" ReadOnly="true" TextMode="MultiLine"
runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnFerrousSystem" Width="150px" runat="server" Text="Edit"
OnClick="Edit_Text" CommandArgument="3" />
</td>
</tr>
<tr>
<td>
Coke System
</td>
<td>
<asp:TextBox ID="txtCokeSystem" Width="400px" ReadOnly="true" TextMode="MultiLine"
runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnCokeSystem" Width="150px" runat="server" Text="Edit"
OnClick="Edit_Text" CommandArgument="4" />
</td>
</tr>
<tr>
<td>
Coal Yards
</td>
<td>
<asp:TextBox ID="txtCoalYards" Width="400px" ReadOnly="true" TextMode="MultiLine"
runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnCoalYards" Width="150px" runat="server" Text="Edit"
OnClick="Edit_Text" CommandArgument="5" />
</td>
</tr>
<tr>
<td>
Screenhouse
</td>
<td>
<asp:TextBox ID="txtScreenhouse" Width="400px" ReadOnly="true" TextMode="MultiLine"
runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnScreenhouse" Width="150px" runat="server" Text="Edit"
OnClick="Edit_Text" CommandArgument="6" />
</td>
</tr>
<tr>
<td>
Process Plant
</td>
<td>
<asp:TextBox ID="txtProcessPlant" Width="400px" ReadOnly="true" TextMode="MultiLine"
runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnProcessPlant" Width="150px" runat="server" Text="Edit"
OnClick="Edit_Text" CommandArgument="7" />
</td>
</tr>
<tr>
<td>
New Mill
</td>
<td>
<asp:TextBox ID="txtNewMill" Width="400px" ReadOnly="true" TextMode="MultiLine" runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnNewMill" Width="150px" runat="server" Text="Edit"
OnClick="Edit_Text" CommandArgument="8" />
</td>
</tr>
<tr>
<td>
Streamphases
</td>
<td>
<asp:TextBox ID="txtStreamphases" Width="400px" ReadOnly="true" TextMode="MultiLine"
runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnStreamphases" Width="150px" runat="server" Text="Edit"
OnClick="Edit_Text" CommandArgument="9" />
</td>
</tr>
<tr>
<td>
Furnace Silos Injection
</td>
<td>
<asp:TextBox ID="txtFurnaceSilosInjection" Width="400px" ReadOnly="true" TextMode="MultiLine"
runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnFurnaceSilosInjection" Width="150px" runat="server" Text="Edit"
OnClick="Edit_Text" CommandArgument="10" />
</td>
</tr>
<tr>
<td>
CompressedAir
</td>
<td>
<asp:TextBox ID="txtCompressedAir" Width="400px" ReadOnly="true" TextMode="MultiLine"
runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnCompressedAir" Width="150px" runat="server" Text="Edit"
OnClick="Edit_Text" CommandArgument="11" />
</td>
</tr>
<tr>
<td>
Planned Maintenance
</td>
<td>
<asp:TextBox ID="txtPlannedMaintenance" Width="400px" ReadOnly="true" TextMode="MultiLine"
runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnPlannedMaintenance" Width="150px" runat="server" Text="Edit"
OnClick="Edit_Text" CommandArgument="12" />
</td>
</tr>
<tr>
<td>
Notifications Raised
</td>
<td>
<asp:TextBox ID="txtNotificationsRaised" Width="400px" ReadOnly="true" TextMode="MultiLine"
runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnNotificationsRaised" Width="150px" runat="server" Text="Edit"
OnClick="Edit_Text" CommandArgument="13" />
</td>
</tr>
<tr>
<td>
Manning
</td>
<td>
<asp:TextBox ID="txtManning" Width="400px" ReadOnly="true" TextMode="MultiLine" runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnManning" Width="150px" runat="server" Text="Edit"
OnClick="Edit_Text" CommandArgument="14" />
</td>
</tr>
<tr>
<td>
ShiftHandover
</td>
<td>
<asp:TextBox ID="txtShiftHandover" Width="400px" ReadOnly="true" TextMode="MultiLine"
runat="server" OnClick="Edit_Text"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnShiftHandover" Width="150px" runat="server" Text="Edit"
OnClick="Edit_Text" CommandArgument="15" />
</td>
</tr>
</table>
When the button is pressed the following method is called:
protected void Edit_Text(object sender, EventArgs e)
{
Button btn = sender as Button;
if (btn != null)
{
editField = btn.ID.Replace("btn", "");
btn.Visible = false;
//Getting and storing rowindex
TextBox txt = this.FindControl("txt" + editField) as TextBox;
txt.ReadOnly = false;
txt.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFFB2");
txt.Focus();
comment = txt.Text;
//Further methods unnecessary for this question
}
}
As you can see, the prefix is stripped from the controls and then added in when necessary to find the right control at the point, therefore the string for the FindControl method is correct and should return a a TextBox.
However, by stepping through I can see that it is passing an empty TextBox instance rather than the expected result or a null.
I have already tried the findcontrol function on the table itself, I used 'this' as a second check but both yielded the same results.
I suspect the html table is a problem as in another method I am having issues finding a button manually in the html table:
Button editbtn = tblMain.Rows[GetRowIndex(hdnRowIndex.Value)].Cells[2].Controls[0] as Button;
Which is returning a null despite there always being a cell button in that cell for each row
EDIT - Here is the binding that is performed
private void bindData(DataTable dt)
{
if (dt.Rows.Count > 0)
{
foreach (DataColumn col in dt.Columns)
{
TextBox txt = tblMain.FindControl("txt" + col.ColumnName) as TextBox;
if (txt != null)
{
txt.Text = dt.Rows[0][col].ToString();
}
}
}
}
Your problem is here:
this.FindControl("txt" + editField) as TextBox;
FindControl is not recursive and can only find immediate children of the control it was called on. Here you are calling it of the page instance, so it only seeks through immediate children of the page in the control tree (presumably this is only a form element).
What you need to do is to pick a server-side control right above your textboxes/buttons and run FindControl of it. For instance, let's say it is your table, which is declared as:
<table>
<%--...all the content from the post goes here...--%>
</table>
Make this table server-side:
<table id="TheTable" runat="server">
and then call FindControl on this table:
TextBox txt = TheTable.FindControl("txt" + editField) as TextBox;
You can of course keep the table client-side, and use some control that wraps this table. Just make sure that in between your control and the textbox in question there are no other server-side controls.
<table style="width:100%; margin-left:auto; margin-right:auto;" id="tblTst" runat="server">
<tr>
<td>
<h2>
Vehicle Information
<asp:Label ID="lblTest" Text="THis is a Test" ForeColor="Salmon" runat="server" />
<asp:Button ID="btnTest" CommandArgument="0" OnClick="btnTest_click" runat="server" Text="Test" />
</h2>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblTst2" Text="This is also a Test" ForeColor="Salmon" runat="server" />
<asp:Button ID="btnTest2" CommandArgument="1" OnClick="btnTest_click" runat="server" Text="Test" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblMsg" runat="server" />
</td>
</tr>
</table>
protected void btnTest_click(object sender, EventArgs e)
{
Button btn = sender as Button;
string comment = "";
string editField = "";
if (btn != null)
{
editField = btn.ID.Replace("btn", "");
btn.Visible = false;
//Getting and storing rowindex
foreach(HtmlTableCell cl in tblTst.Rows[Convert.ToInt32(btn.CommandArgument)].Cells)
{
foreach (Control ctrl in cl.Controls)
{
if(ctrl is Label)
{
Label txt = new Label();
txt = ctrl as Label;
txt.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFFB2");
txt.Focus();
comment = txt.Text;
lblMsg.Text = comment;
return;
}
}
}
//Further methods unnecessary for this question
}
}
}
This is what I came up with, this does work; however it is a fairly dirty way to do this, I'm sure you can clean it up quite a bit. This is only intended as a starting point. The big different is that you need to look in the cell for the control not in the table or page, also the cell is an htmltable cell not just a table cell.

Tabcontainer issue with rendering the tab stripe

enter code![as you can see here the tabs are half eaten!][1] here
Hello, I have a problem with showing the tab stripe I don't know what is wrong with it. can any one help? when I run this the tabs are not fully shown I have three tabs and I tried this with different browsers but the issue persists.
below is a sample of the code:
<asp:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="0" BackColor="#62A5C8" BorderStyle="none" ForeColor="#ffffff" Height="450px" server="" Style=" background-color:#012F54; margin-top: 0px;" Width="100%" ScrollBars="Both" Font-Size="Medium"
ToolTip="Move between tabs to fill-in all information">
<asp:TabPanel runat="server" HeaderText="Student Information" ID="TabPanel1">
<ContentTemplate>
<asp:UpdatePanel ID="updatePanel1" runat="server">
<ContentTemplate>
<table class="auto-style15">
<tr>
<td class="auto-style27">
<br />
<span class="auto-style117">First Name:<br /> </span>
</td>
<td class="auto-style21">
<br />
<strong>
<asp:TextBox ID="TextBox1" runat="server" BorderColor="Black" BorderStyle="Solid" Height="23px" Width="198px" CssClass="auto-style116"></asp:TextBox>
</strong>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style28"> </td>
<td class="auto-style21"> </td>
<td> </td>
</tr>
<tr>
<td class="auto-style117">Last Name:</td>
<td class="auto-style24"><strong>
<asp:TextBox ID="TextBox2" runat="server" BorderStyle="Solid" Height="21px" Width="198px" BorderColor="Black" CssClass="auto-style116"></asp:TextBox>
</strong>
</td>
<td class="auto-style25"></td>
</tr>
<tr>
<td class="auto-style17"></td>
<td class="auto-style20"></td>
<td class="auto-style18"></td>
</tr>
<tr>
<td class="auto-style27" style="font-size: small; color: #043d5d">Student ID:</td>
<td class="auto-style21"><strong>
<asp:TextBox ID="TextBox3" runat="server" BorderStyle="Solid" Height="23px" Width="198px" BorderColor="Black" CssClass="auto-style116"></asp:TextBox>
</strong>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style28"> </td>
<td class="auto-style21"> </td>
<td> </td>
</tr>
<tr>
<td class="auto-style27" style="font-size: small; color: #043d5d">Department:</td>
<td class="auto-style21"><strong>
<asp:DropDownList ID="DropDownList1" runat="server" Height="27px" Width="295px" style="margin-left:7px;">
<asp:ListItem>Cardiac Technology</asp:ListItem>
<asp:ListItem>Clinical Laboratory Sciences</asp:ListItem>
</asp:DropDownList>
</strong>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style28"> </td>
<td class="auto-style21"> </td>
<td> </td>
</tr>
<tr>
<td class="auto-style28"> </td>
<td class="auto-style21"> </td>
</tr>
<tr>
<td class="auto-style28"> </td>
<td class="auto-style21"> </td>
</tr>
<tr>
<td class="auto-style28"> </td>
<td class="auto-style21"> </td>
<td>
<asp:Button ID="Button1" runat="server" BackColor="#4DA3B0" BorderColor="#4DA3B0" Font-Bold="False" Font-Size="12pt" ForeColor="White" Height="36px" style="margin-left:10px;" Text="Next ►" Width="166px" />
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</ContentTemplate>
</asp:TabPanel>
<asp:TabPanel ID="TabPanel2" runat="server" HeaderText="Graduate Form">
<ContentTemplate>
**
Hi brother use below code
first add refrence at the top of the page
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
then use below code
<asp:ScriptManager runat="server" ID="sc">
</asp:ScriptManager>
<asp:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="0" BackColor="#62A5C8"
BorderStyle="none" ForeColor="#ffffff" Height="450px" server="" Style="background-color: #012F54;
margin-top: 0px;" Width="100%" ScrollBars="Both" Font-Size="Medium" ToolTip="Move between tabs to fill-in all information">
<asp:TabPanel runat="server" HeaderText="Student Information" ID="TabPanel1">
<ContentTemplate>
<table class="auto-style15">
<tr>
<td class="auto-style27">
<br />
<span class="auto-style117">First Name:<br />
</span>
</td>
<td class="auto-style21">
<br />
<strong>
<asp:TextBox ID="TextBox1" runat="server" BorderColor="Black" BorderStyle="Solid"
Height="23px" Width="198px" CssClass="auto-style116"></asp:TextBox>
</strong>
</td>
<td>
</td>
</tr>
<tr>
<td class="auto-style28">
</td>
<td class="auto-style21">
</td>
<td>
</td>
</tr>
<tr>
<td class="auto-style117">
Last Name:
</td>
<td class="auto-style24">
<strong>
<asp:TextBox ID="TextBox2" runat="server" BorderStyle="Solid" Height="21px" Width="198px"
BorderColor="Black" CssClass="auto-style116"></asp:TextBox>
</strong>
</td>
<td class="auto-style25">
</td>
</tr>
<tr>
<td class="auto-style17">
</td>
<td class="auto-style20">
</td>
<td class="auto-style18">
</td>
</tr>
<tr>
<td class="auto-style27" style="font-size: small; color: #043d5d">
Student ID:
</td>
<td class="auto-style21">
<strong>
<asp:TextBox ID="TextBox3" runat="server" BorderStyle="Solid" Height="23px" Width="198px"
BorderColor="Black" CssClass="auto-style116"></asp:TextBox>
</strong>
</td>
<td>
</td>
</tr>
<tr>
<td class="auto-style28">
</td>
<td class="auto-style21">
</td>
<td>
</td>
</tr>
<tr>
<td class="auto-style27" style="font-size: small; color: #043d5d">
Department:
</td>
<td class="auto-style21">
<strong>
<asp:DropDownList ID="DropDownList1" runat="server" Height="27px" Width="295px" Style="margin-left: 7px;">
<asp:ListItem>Cardiac Technology</asp:ListItem>
<asp:ListItem>Clinical Laboratory Sciences</asp:ListItem>
</asp:DropDownList>
</strong>
</td>
<td>
</td>
</tr>
<tr>
<td class="auto-style28">
</td>
<td class="auto-style21">
</td>
<td>
</td>
</tr>
<tr>
<td class="auto-style28">
</td>
<td class="auto-style21">
</td>
</tr>
<tr>
<td class="auto-style28">
</td>
<td class="auto-style21">
</td>
</tr>
<tr>
<td class="auto-style28">
</td>
<td class="auto-style21">
</td>
<td>
<asp:Button ID="Button1" runat="server" BackColor="#4DA3B0" BorderColor="#4DA3B0"
Font-Bold="False" Font-Size="12pt" ForeColor="White" Height="36px" Style="margin-left: 10px;"
Text="Next ►" Width="166px" />
</td>
</tr>
</table>
</ContentTemplate>
</asp:TabPanel>
<asp:TabPanel ID="TabPanel2" runat="server" HeaderText="Graduate Form">
<ContentTemplate>
</ContentTemplate>
</asp:TabPanel>
</asp:TabContainer>

TextBox validation is empty or 11 digits

I have a textbox in my form which must be validated only if is empty or contains 11 digits.
I tried every solution found on internet but nothing happens.
<script type="text/javascript">
$(document).ready(function() {
$('#ctl00_CPH1_tax_id').attr("placeholder", "1234567890");
$('#ctl00_CPH1_citizen_id').attr("placeholder", "12345678901");
$('#ctl00_CPH1_e_posta').attr("placeholder", "ahmet.mehmet#sirket.com.tr");
//var isbp = $('#ctl00_CPH1_IsBP');
var name = $('#ctl00_CPH1_name').val();
var code = $('#ctl00_CPH1_code').val();
var dropdownlist2 = $('select:first');
var tax_id = $('#ctl00_CPH1_tax_id').val();
var citizen_id = $('#ctl00_CPH1_citizen_id').val();
var email1 = $('#ctl00_CPH1_e_posta').val();
var isbp = $('input[type=checkbox]:first');
var dropdownlist1 = $('select:last');
var isactive = $('input[type=checkbox]:last');
//var dropdownlist1 = $('#ct100_CPH1_DropDownList1');
var tax_id_check = /^\d{10}$/.test(tax_id);
var citizen_id_check = /^$|^\d{11}$/.test(citizen_id);
var email1_check = /^[a-zA-Z0-9._]+#([a-zA-Z0-9.]|[a-zA-Z0-9]+\-[a-zA-Z0-9.])+\.[a-zA-Z]{2,6}$/.test(email1);
dropdownlist1.attr("disabled", "disabled");
isactive.attr("disabled", "disabled");
$(isbp).click(function(e) {
if (isbp.is(':checked')) {
dropdownlist1.removeAttr("disabled");
isactive.removeAttr("disabled");
}
else {
isactive.attr("disabled", "disabled");
dropdownlist1.attr("disabled", "disabled");
}
});
$("#ctl00_CPH1_Button1").click(function(e) {
//console.log(drop2);
//var name_status = /^[a-zA-Z][a-zA-Z]*$/.test(name);
//var code_status = /^[a-zA-Z][a-zA-Z]*$/.test(code);
//var IsBP = $('#ctl00_CPH1_IsBP');
//if (name.length < 3 || name_status == false) {
// e.preventDefault();
// alert('İsim sadece karakterlerden oluşmalı ve en az iki karakter olmalı!');
//}
//if (code.length < 3 || code_status == false) {
// e.preventDefault();
// alert('Soyisim sadece karakterlerden oluşmalı ve en az iki karakter olmalı!');
//}
if (name.length == 0) {
e.preventDefault();
alert("İsim girilmedi!");
}
if (code.length == 0) {
e.preventDefault();
alert("Şirket Kodu girilmedi!");
}
if (dropdownlist2.val() == 0) {
e.preventDefault();
alert("Ülke seçilmedi!");
}
if (citizen_id.length != 0) {
e.preventDefault();
alert("Vatandaşlık numarası onbir haneli olmalı!");
}
if (tax_id.length == 0) {
e.preventDefault();
alert("Vergi numarası girilmedi!");
}
else if (!tax_id_check) {
e.preventDefault();
alert("Vergi numarası on haneli olmalı!");
}
if (email1 != "" && !email1_check) {
e.preventDefault();
alert("E-Posta geçerli değil!");
}
if (isbp.is(':checked') && dropdownlist1.val() == 0) {
e.preventDefault();
alert("İş ortağı tipi seçilmedi!");
}
});
});
This is my script and aspx follows:
<form id="form1" runat="server">
<div>
<center>
<table>
<tr>
<td colspan="3" align="center">
Şirket Giriş Ekranı</td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td align="left">
<asp:Label ID="Label1" runat="server" Text="İsim"></asp:Label>
</td>
<td colspan="2">
<asp:TextBox ID="name" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="left">
<asp:Label ID="Label2" runat="server" Text="Şirket Kodu"></asp:Label>
</td>
<td colspan="2">
<asp:TextBox ID="code" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="left">
<asp:Label ID="Label3" runat="server" Text="Ülke"></asp:Label>
</td>
<td class="style4" colspan="2">
<asp:DropDownList ID="DropDownList2" runat="server"
onselectedindexchanged="DropDownList2_SelectedIndexChanged">
</asp:DropDownList>
</td>
</tr>
<tr>
<td align="left">
<asp:Label ID="Label4" runat="server" Text="Vergi No"></asp:Label>
</td>
<td colspan="2">
<asp:TextBox ID="tax_id" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="left">
<asp:Label ID="Label5" runat="server" Text="Vergi Dairesi"></asp:Label>
</td>
<td colspan="2">
<asp:TextBox ID="tax_office" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="left">
<asp:Label ID="Label6" runat="server" Text="Vatandaşlık No"></asp:Label>
</td>
<td colspan="2">
<asp:TextBox ID="citizen_id" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="left">
<asp:Label ID="Label7" runat="server" Text="Adres"></asp:Label>
</td>
<td colspan="2">
<asp:TextBox ID="address" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="left">
<asp:Label ID="Label14" runat="server" Text="E-posta"></asp:Label>
</td>
<td colspan="2">
<asp:TextBox ID="e_posta" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="left">
<asp:Label ID="Label15" runat="server" Text="Telefon"></asp:Label>
</td>
<td colspan="2">
<asp:TextBox ID="telefon" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="left">
<asp:Label ID="Label16" runat="server" Text="Fax"></asp:Label>
</td>
<td colspan="2">
<asp:TextBox ID="faks" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="left">
<asp:Label ID="Label10" runat="server" Text="İş Ortağım"></asp:Label>
</td>
<td colspan="2">
<asp:CheckBox ID="IsBP" runat="server" oncheckedchanged="IsBP_CheckedChanged"/> <!--AutoPostBack="true"-->
<asp:Label ID="Label11" runat="server" Text="Evet"></asp:Label>
</td>
</tr>
<tr>
<td align="left">
<asp:Label ID="Label12" runat="server" Text="İş Ortağı Tipi"></asp:Label>
</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
</td>
<td>
<asp:CheckBox ID="IsActive" runat="server"
oncheckedchanged="IsActive_CheckedChanged" />
<asp:Label ID="Label13" runat="server" Text="Aktif"></asp:Label>
</td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td>
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Geri" />
</td>
<td colspan="2" align="center">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Kaydet" />
</td>
</tr>
</table>
<br />
</center>
</div>
</form>
I tried jquery older versions like 1.8.3 which is stable instead of 1.9.1
Also jquery.validate.
When I run the code on chrome and inspect element on console everything runs properly.
But my code does not show any alerts.
"==" works but "!=" or ">" not.
Thanks in advance!
not working textboxes are e_mail and citizen_id. other textboxes work properly.
Use the ASP.NET validator with a regular expression akin to
"(\d{11})?"
<asp:RegularExpressionValidator ControlToValidate="citizen_id"
ValidationExpression="(\d{11})?" runat="server"
Text="Must be 11 digits or empty" />
This will prevent a postback if the textbox "citizen_id" is anything but "" or an 11 digit number.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.regularexpressionvalidator.aspx

Categories