I don't understand why I am getting alert "Empty" when I type some numbers or leave a empty? Can you help me?
My Code:
</script>
<script type="text/javascript">
function tell() {
var v = true;
if (document.getElementsByName("TextBox33").length == 0)
alert("Empty");
v = false;
return v;
}
</script>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Insert" OnClientClick="javaScript: return tell();"
<asp:TextBox ID="TextBox33" onkeypress="return isNumberKey(event)" runat="server" Text='<%# Bind("imei") %>'></asp:TextBox>
I am adding more html:
<asp:DetailsView ID="DetailsView1" runat="server" DefaultMode="Insert" AutoGenerateRows="False" DataKeyNames="id" DataSourceID="SqlDataSource1" Height="50px" Width="286px" OnItemInserting="DetailsView1_ItemInserting" OnDataBound="DetailsView1_DataBound" OnItemInserted="DetailsView1_ItemInserted">
<asp:TemplateField HeaderText="IMEI" SortExpression="imei">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("imei") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox33" onkeypress="return isNumberKey(event)" CssClass="TextBox33" runat="server" Text='<%# Bind("imei") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("imei") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
You're calling getElementsByName which returns al array of DOM elements. You're then taking the length of that, which will always be more than 0 because the element exists.
Try adding a [0] after the getElementsByName call, and then change .text to .value.
Also so you're aware, you need to put the alert and v = false inside brackets. You can only skip brackets if you're executing one statement.
Here's the revised code:
if (document.getElementById("TextBox33").value.length == 0){
alert("Oh no I'm empty!");
v = false;
}
Update: I also just noticed that you didn't actually give the textbox a name of TextBox33, you gave it an ID. I've updated the code.
Related
My dataset return this table link.How can i use If else conditons in aspx page?I want if the File_path is Null or empty then image not show because if File_Path is empty then it give me error,I want if File_Path is not empty then image will be show.Following error
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Here is my code
<asp:DataList ID="DataList2" runat="server" Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" RepeatLayout="Table">
<ItemTemplate>
<div class="<%#GetStyleForMsgList(Eval("MsgSender").ToString()) %> MainChatListClass">
<asp:Label ID="Label1" runat="server" Text='<%# GetPerfactName(Eval("MsgSender").ToString()) %>'></asp:Label>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("ChatMsg") %>'></asp:Label>
<asp:Label ID="lblfile" runat="server" Visible ="false" Text='<%# Eval("File_Path") %>'></asp:Label>
<% if (Eval("File_Path") != null)
{%>
<asp:Image ID ="img" runat ="server" ImageUrl ='<%# Eval("File_Path") %>' />
<% } %>
</div>
</ItemTemplate>
</asp:DataList>
I have a gridview written in asp.net (c#), the problem is when try to read data from the textbox return null.
<asp:GridView ID="GridView1" runat="server" DataKeyNames="Week#/Day"
OnRowDataBound="GridView1_RowDataBound"
onrowediting="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating" AutoGenerateColumns="false"
OnPageIndexChanging="GridView1_PageIndexChanging"
OnRowCancelingEdit="GridView1_RowCancelingEdit" AllowPaging="true"
PageSize="4" >
<Columns>
<asp:ButtonField ButtonType="Link" CommandName="Update" text="Update" />
<asp:ButtonField ButtonType="Link" CommandName="Edit" text="Edit"/
<asp:TemplateField HeaderText="Week#/Day" InsertVisible="False"
SortExpression="Week#/Day">
<EditItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%#
Eval("Week#/Day") %>'></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Saturday" SortExpression="Saturday">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("Saturday") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server"
Text='<%# Bind("Saturday") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sunday" SortExpression="Sunday">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"
Text='<%# Bind("Sunday") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label8" runat="server"
Text='<%# Bind("Sunday") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Monday" SortExpression="Monday">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server"
Text='<%# Bind("Monday") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label9" runat="server"
Text='<%# Bind("Monday") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Tuesday" SortExpression="Tuesday">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server"
Text='<%# Bind("Tuesday") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label10" runat="server"
Text='<%# Bind("Tuesday") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Wednesday"
SortExpression="Wednesday">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server"
Text='<%# Bind("Wednesday") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label11" runat="server"
Text='<%# Bind("Wednesday") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I am fill it using datatable on edit call this method
protected void GridView1_RowEditing(object sender,GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData();
}
and on update call update method
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//Retrieve the table from the session object.
DataTable dt = (DataTable)Session["All_Topics"];
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
TextBox d1, d2, d3, d4, d5;
d1 = (TextBox)row.FindControl("TextBox1");
d2 = (TextBox)row.FindControl("TextBox2");
d3 = (TextBox)row.FindControl("TextBox3");
d4 = (TextBox)row.FindControl("TextBox4");
d5 = (TextBox)row.FindControl("TextBox5");
dt.Rows[row.DataItemIndex]["Saturday"] = d1.Text;
dt.Rows[row.DataItemIndex]["Sunday"] = d2.Text;
dt.Rows[row.DataItemIndex]["Monday"] = d3.Text;
dt.Rows[row.DataItemIndex]["Tuesday"] = d4.Text;
dt.Rows[row.DataItemIndex]["Wednesday"] = d5.Text;
}
the problem is when read data from the textbox it is always null. How can I solve this problem.
Hey please Make sure that do not re-binding the GridView on the PostBack of the page. This may be the cause of problem.
Always bind your grid on page load in side the below code.
if (!Page.IsPostBack ){
// Code to bind the Grid
// BindData();
}
when you are click on Row Update Command it is firstly go to the page load event and on page load if you are not binding your grid like the given manner your grid is rebind and you will get null from text box.
Hope it will helps you.
I've started creating an online quiz. I am using a DetailsView linked to a sqlDataSource.
Here's my code:
<asp:DetailsView ID="QuestionDetails" runat="server" AutoGenerateRows="False" DataKeyNames="QuestionID,QuizID" DataSourceID="SqlDataSource_Quiz">
<Fields>
<asp:TemplateField HeaderText="Text_Eng" SortExpression="Text_Eng" ShowHeader="False">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Text_Eng") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Text_Eng") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Text_Eng") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:RadioButton ID="Option1" AccessKey="1" runat="server" Text='<%# Bind("Opt_1") %>' GroupName="AnswerOptions" AutoPostBack="true" OnCheckedChanged="Option_CheckedChanged" />
<br />
<asp:RadioButton ID="Option2" AccessKey="2" runat="server" Text='<%# Bind("Opt_2") %>' GroupName="AnswerOptions" AutoPostBack="true" OnCheckedChanged="Option_CheckedChanged"/>
<br />
<asp:RadioButton ID="Option3" AccessKey="3" runat="server" Text='<%# Bind("Opt_3") %>' GroupName="AnswerOptions" AutoPostBack="true" OnCheckedChanged="Option_CheckedChanged"/>
<br />
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
Note that i am not displaying all data retrieved from the sqlDataSource (there are other fields/columns as well) in the DetailsView.
Now, in my code behind, i am showing the next record by changing the PageIndex of the DetailsView upon clicking a button (NextButton) but at the same time, i need to retrieve some data of the currently displayed record so I've put the following in the button's click handler:
protected void NextButton_Click(object sender, EventArgs e)
{
try
{
// Save off previous answers
DataRowView dr = (DataRowView)QuestionDetails.DataItem;
// Create Answer object to save values
Answer a = new Answer();
a.QuestionID = dr["QuestionID"].ToString();
a.CorrectAnswer = dr["CorrectAnswer"].ToString();
a.UserAnswer = selectedOptionRB.AccessKey.ToString();
ArrayList al = (ArrayList)Session["userAnswers"];
al.Add(a);
Session.Add("userAnswers", al);
}
catch (Exception ex)
{
Label2.Text = "Exception!";
}
if (QuestionDetails.PageIndex == QuestionDetails.PageCount - 1)
{
NextButton.Enabled = false;
}
else
{
QuestionDetails.PageIndex += 1;
NextButton.Enabled = false;
}
if (QuestionDetails.PageIndex == QuestionDetails.PageCount - 1)
{
NextButton.Text = "Finish the Quiz";
}
}
So when i run the code, i get "Exception!" displayed in my Label2 which i setup for testing.
What am i doing wrong?
I have a GridView inside an UpdatePanel and use javascript to toggle the visibility of a row when the user clicks on an expand button. There is quite a bit of information above the GridView, but I want only the information in the respective UpdatePanel to update so that the screen will stay as is, but the JavaScript call is causing an entire screen refresh and the window jumps back to the top.
The code I'm using for the GridView actually comes from the ExtGridView control on CodeProject (http://www.codeproject.com/Articles/12299/ExtGridView) - it turns the last asp:TemplateField of a GridView into a new row beneath the other items.
I'm new to JavaScript and fairly new to ASP.NET, so I may be missing something simple. Is there a way to keep the JavaScript refresh to just the respective UpdatePanel that caused it?
Here is the JavaScript code:
<script type="text/javascript">
//<![CDATA[
function TglRow(ctl)
{
var row = ctl.parentNode.parentNode;
var tbl = row.parentNode;
var crow = tbl.rows[row.rowIndex + 1];
var ihExp = ctl.parentNode.getElementsByTagName('input').item(0);
tbl = tbl.parentNode;
var expandClass = tbl.attributes.getNamedItem('expandClass').value;
var collapseClass = tbl.attributes.getNamedItem('collapseClass').value;
var expandText = tbl.attributes.getNamedItem('expandText').value;
var collapseText = tbl.attributes.getNamedItem('collapseText').value;
if (crow.style.display == 'none')
{
crow.style.display = '';
ctl.innerHTML = collapseText;
ctl.className = collapseClass;
ihExp.value = '1';
}
else
{
crow.style.display = 'none';
ctl.innerHTML = expandText;
ctl.className = expandClass;
ihExp.value = '';
}
}//]]>
</script>
And here is an excerpt from the GridView:
<asp:UpdatePanel ID="UpdatePanelChapter11" runat="server"
ChildrenAsTriggers="false" UpdateMode="Conditional">
<ContentTemplate>
<cc1:ExtGridView ID="gvChapter11" runat="server" AutoGenerateColumns="False" DataSourceID="odsChapter11"
DataKeyNames="pkChapter11ID" ShowFooter="True" SkinID="GridViewSKin" Width="85%"
onrowcommand="gvChapter11_RowCommand"
onrowdatabound="gvChapter11_RowDataBound"
onrowupdating="gvChapter11_RowUpdating"
CollapseButtonCssClass="GridCollapseButton"
ExpandButtonCssClass="GridExpandButton" CollapseButtonText=""
ExpandButtonText="" onrowcreated="gvChapter11_RowCreated">
<Columns>
<asp:TemplateField HeaderText="Name of<br/>Party" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="tbName" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
<br /><asp:RequiredFieldValidator ID="tbNameValidator" runat="server" ErrorMessage="*Name Required" ControlToValidate="tbName"
Display="Dynamic" CssClass="Error" ValidationGroup="SaveChapter11Validation"></asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="tbName" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
<br /><asp:RequiredFieldValidator ID="tbNameValidator" runat="server" ErrorMessage="*Name Required" ControlToValidate="tbName"
Display="Dynamic" CssClass="Error" ValidationGroup="NewChapter11Validation"></asp:RequiredFieldValidator>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:LinkButton CssClass="Button" ID="SaveLink" runat="server" CommandName="Update" Text="Save" ValidationGroup="SaveChapter11Validation"></asp:LinkButton>
<asp:LinkButton CssClass="Button" ID="CancelLink" runat="server" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton CssClass="Button" ID="EditLink" runat="server" CommandName="Edit" Text="Edit"></asp:LinkButton>
<asp:LinkButton CssClass="Button" ID="DeleteLink" runat="server" CommandName="Delete" Text="Delete"
OnClientClick="return confirm('Are you sure you want to delete this entry?');" ></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton CssClass="Button" ID="AddLink" runat="server" CommandName="Insert" Text="<<< Add" ValidationGroup="NewChapter11Validation"></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Plan">
<EditItemTemplate>
<div class="ExtGridRow" style="vertical-align:top;">Plan: </div>
<asp:TextBox ID="tbPlan" runat="server" Text='<%# Bind("Plan") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<div class="ExtGridRow" style="vertical-align:top;">Plan: </div>
<asp:Label ID="Label9" runat="server" Text='<%# Eval("Plan") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<div class="ExtGridRow" style="vertical-align:top;">Plan: </div>
<asp:TextBox ID="tbPlan" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</cc1:ExtGridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="gvChapter11" EventName="DataBound" />
</Triggers>
</asp:UpdatePanel>
Well shoot... a little more playing around with the ExtGridView code and I figured it out. The expand/toggle button that calls the javascript also had an HRef assigned, acting as a link, so when it was clicked it would not only call the JavaScript but also acted as if a link was clicked, causing the page refresh.
The culprit code from the ExtGridView:
// ...
else if (RowType == DataControlRowType.DataRow || RowType == DataControlRowType.Footer)
{
_expCell = new TableCell();
_ctlExpand = new HtmlAnchor();
//_ctlExpand.HRef = "#"; -- Commenting this out worked!
_ctlExpand.Attributes["onclick"] = "TglRow(this);";
_ihExp = new HtmlInputHidden();
_ihExp.ID = "e" + this.DataItemIndex.ToString();
_expCell.Controls.Add(_ctlExpand);
_expCell.Controls.Add(_ihExp);
}
if (_expCell != null)
{
_expCell.Width = Unit.Pixel(20);
Cells.AddAt(0, _expCell);
}
Rate AnodeRate TotalRate
100 100 200
txtrate,txtanoderate n txttatalrate are added in ItemTemplate of gridview.
rate field is in database table while anoderate n totalrate not in database table
,also totalrate is readonly so that user can see only its addition,this addition will b passed to txtrate field n wil get bind that value in database.
also i took
<asp:HiddenField ID="hdnTotalRt" runat="server" /> as it is readonly..
sample code:
enter code here
<asp:TemplateField HeaderText="Standard Rate">
<ItemTemplate>
<asp:TextBox ID="txtRate" runat="server" BorderWidth="0.5pt" Text='<%# Bind("rate") %>' BorderColor="green" BackColor="#ffffcc" Width="40px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txtRate" runat="server" ValidationGroup="ValAtSave" >*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ControlToValidate="txtRate" runat="server" ValidationExpression="[0-9]*.?[0-9]*" ValidationGroup="ValAtSave">
Nos Pls.
</asp:RegularExpressionValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="AnodeRate">
<ItemTemplate>
<asp:TextBox ID="txtAnodeRate" runat="server" BorderWidth="0.5pt"
BorderColor="green" BackColor="#ffffcc" Width="40px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total Rate" >
<ItemTemplate>
<asp:TextBox ID="txtTotalRate" runat="server" onchange="javascript:calculate_total_rate()" BorderWidth="0.5pt" Enabled="false"
BorderColor="green" BackColor="#ffffcc" Width="40px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
function calculate_total_rate()
{
document.getElementById('<%=txtTotalRate%>').value = ( document.getElementById('<%=txtRate.ClientID %>').value ) + (document.getElementById('<%=txtAnodeRate.ClientID %>').value );
document.getElementById('<%=hdnTotalRt.ClientID %>').value= document.getElementById('<%=txtTotalRate.ClientID %>').value;
}
It is showing me error as The name "txtRate"does not exist in d current context
can you try with this code, (I have
replaced '=' in
document.getElementById('<%=txtTotalRate%>')
and
document.getElementById('<%=txtAnodeRate.ClientID
%>' by '#')
function calculate_total_rate()
{
document.getElementById('<%#txtTotalRate.ClientID%>').value = ( document.getElementById('<%=txtRate.ClientID %>').value ) + (document.getElementById('<%#txtAnodeRate.ClientID %>').value );
document.getElementById('<%=hdnTotalRt.ClientID %>').value= document.getElementById('<%#txtTotalRate.ClientID %>').value;
}