|
The following code is used for a user control. What it does is if the user presses the arrow up key when the focus is on the input box it will increment the value by one. When I put one of these user controls on a web form it works good. But if I put a second user control on the same web page and when pressing the arrow up key when the focus is on it it increments the value on the first user control. From the code I can see why this happens but how can I have it increment the value on the input box that has the focus?
<%@ Control Language="C#"%>
function TimeEntryHoursOnClick()
{
if (event.keyCode == 38)
document.getElementById("Text1").value = parseFloat(document.getElementById("Text1").value) + 1
}
|
|
|
|
|
You're have to prefix the function name and the control name with the unique id of the user control, e.g.
<%@ Control Language="C#"%>
<script type="text/javascript">
function <%=this.ClientId%>_TimeEntryHoursOnClick()
{
if (event.keyCode == 38)
document.getElementById("<%=this.ClientId%>_Text1").value = parseFloat(document.getElementById("<%=this.ClientId%>_Text1").value) + 1
}
</script>
<input id="<%=this.ClientId%>_Text1" önkeyup="<%=this.ClientId%>_TimeEntryHoursOnClick()" type="text" />
There are better ways of doing this tho such as moving the javascript function outside of the user control and passing the object being changed to the function, i.e.
<script type="text/javascript">
function TimeEntryHoursOnClick(theTextbox)
{
if (event.keyCode == 38)
theTextbox.value = parseFloat(theTextbox.value) + 1
}
</script>
<%@ Control Language="C#"%>
<input id="<%=this.ClientId%>_Text1" önkeyup="TimeEntryHoursOnClick(this)" type="text" />
I haven't tested the code above but it should work.
|
|
|
|
|
Thanks. That worked but I have one other related problem. The user control actually has two input boxes. When TimeEntryHoursOnClick() gets called for Text1 the function also has to reference Text2 as show below. How would I pass that information into the function.
function TimeEntryHoursOnClick(theTextbox)
{
if (event.keyCode == 38)
theTextbox.value = parseFloat(theTextbox.value) + 1
if (event.keyCode == 39)
document.getElementById("Text2").focus();
}
|
|
|
|
|
I am trying to have the button in my gridview create a popup when clicked. I receive this error during runtime after I select "Ok" in the popup.
Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
If I make enableEventValidation false, it runs fine, but that's not a secure way to do it. While performing a google search they allude to there being another way, but do not elaborate. Any ideas?
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<style type="text/css">
body
{
background-color: #CCF0FF;
font-family: Verdana, Arial, Helvetica, Sans-Serif;
font-size: small;
color: Black;
margin-top: 50px;
margin-left: 60px;
margin-right:50px;
border-color: Black;
}
p
{
text-align:right;
}
</style>
<title>Register for Classes</title>
</head>
<body>
<script runat="server">
public string ConnectionPath;
public string register = "Register";
protected void Page_Load(object sender, EventArgs e)
{
try
{
System.Data.DataTable t1 = new System.Data.DataTable();
t1.Columns.Add("LeafName");
t1.Columns.Add("TeacherName");
t1.Columns.Add("SemesterEnds");
t1.Columns.Add("ApplicableTracks");
t1.Rows.Add("Nmap Basics", "Prof. Zhang", "Aug 23 - Nov 3", "ClassDescriptions.aspx#Anchor1");
t1.Rows.Add("Nmap Advanced", "Prof. Scmidt", "Oct 20 - Dec 5", "ClassDescriptions.aspx#Anchor2");
t1.Rows.Add("Nmap Advanced II", "Prof. Strauss", "Jan 5 - Mar 27", "ClassDescriptions.aspx#Anchor3");
t1.Rows.Add("Firewall and Router Fundamentals", "Prof. Williams", "Apr 5 - Jun 26", "ClassDescriptions.aspx#Anchor4");
t1.Rows.Add("Ports and Services", "Prof. Brown", "Oct 20 - Dec 5", "ClassDescriptions.aspx#Anchor5");
SQLQueryClassListings.DataSource = t1;
SQLQueryClassListings.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
</script>
<form id="form1" runat="server">
<div>
<img src="SimBLEND_logo_trans_SMALL.png" />
<a href="main.aspx">Enter</a>
</p>
<br /><br /><br /><br/>
<asp:GridView ID="SQLQueryClassListings" AutoGenerateColumns="false" runat="server" OnRowCommand="ClassGridView_RowCommand"
BorderWidth="1px" BackColor="White" CellPadding="5" BorderColor="Black" RowStyle-BorderColor = "Black"
HeaderStyle-BackColor="#0D69F2" HeaderStyle-ForeColor="White" AlternatingRowStyle-BackColor="#E8E8E8" HeaderStyle-BorderColor="Black" GridLines="Both">
<Columns>
<asp:BoundField HeaderText="Classes" DataField="LeafName" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="250"
ItemStyle-BorderColor="#ADADAD" HeaderStyle-BorderColor ="Black"/>
<asp:BoundField HeaderText="Teacher" DataField="TeacherName" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="200"
ItemStyle-BorderColor="#ADADAD" HeaderStyle-BorderColor ="Black"/>
<asp:BoundField HeaderText="Available" DataField="SemesterEnds" HeaderStyle-HorizontalAlign="Center"
ItemStyle-HorizontalAlign ="Center" ItemStyle-Width="150" ItemStyle-BorderColor="#ADADAD" HeaderStyle-BorderColor ="Black"/>
<asp:HyperLinkField HeaderText="Course Description & Career Tracks" DataNavigateUrlFields="ApplicableTracks"
Text="See Description" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign ="Center" ItemStyle-BorderColor="#ADADAD" HeaderStyle-BorderColor ="Black"/>
<asp:TemplateField HeaderText="Register" HeaderStyle-BorderColor="Black" ItemStyle-BorderColor = "#ADADAD">
<ItemTemplate>
<asp:Button ID="Register" runat="server" CommandName="Register" Text="Register"
OnClientClick="return confirm('You have sucessfully registered!')"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
|
|
|
|
|
I apologize right off for asking this question but I am either searching wrong or it is too late in the day for me to get the idea.
Please, if someone out there can extend a hand, I would greatly appreciate it.
I have several buttons on my page, within a form and I would like each button to perform a specific action (show panels, hide panels, assign variables etc...) when pressed. On the post back, how do I find out which button was pressed so that I may perform the function required. A small snippet or a pointer to a article would work out fine.
Again sorry and thanks.
|
|
|
|
|
If you have button, then each and every button should have an OnClick Event. You need to perform the operation on the Click Event of that button.
BTW, Are your creating button Dynamically ?
Abhijit Jana | Codeproject MVP
Web Site : abhijitjana.net
Don't forget to click "Good Answer" on the post(s) that helped you.
|
|
|
|
|
No, no, no..... It couldn't be that simple!!!! I use OnClick all the time. I knew I wasn't thinking straight.
The buttons are not being generated dynamically. They are set a design time.
I told you that I wasn't in my right mind.
Thanks my friend.
|
|
|
|
|
Let say, You have 3 button in your page, Bt1, Bt2, Bt3. and for each click you want to perform some operation. So what is the problem on that ?
Abhijit Jana | Codeproject MVP
Web Site : abhijitjana.net
Don't forget to click "Good Answer" on the post(s) that helped you.
|
|
|
|
|
No problem now, you've helped me.
|
|
|
|
|
Great!
Abhijit Jana | Codeproject MVP
Web Site : abhijitjana.net
Don't forget to click "Good Answer" on the post(s) that helped you.
|
|
|
|
|
Can i select two or more records randomly from a table in a dataset if yes pls give me a suggestion
|
|
|
|
|
You can access the records in a DataTable by row index, therefore generate 2 random numbers lower than the number of rows in your DataTable and use those to access the records/rows.
|
|
|
|
|
how to generate random numbers
|
|
|
|
|
Take a look at the Random[^] class.
|
|
|
|
|
I'm tasked with helping a developer finish up a web page that collects information about an order and populates some SQL Server tables. ASPX with C#. I'm not a C# guy, but my main job here is to keep him on task, interface with the dept. that will need this, and validate the data design. At present, there are some issues with postbacks. He does a good bit of validation, mostly to ensure fields aren't blank, and posts back upon exit of a good many fields. One problem is retaining focus after postback. The user enters a value, presses tab, and a postback occurs. Instead of the focus moving to the next field, there seems to be no field with focus after the postback, which forces the user to click in the next field with the mouse. So instead of being able to tab to fields as expected, the user keeps having to use the mouse to get the focus to the appropriate field. This is not very desireable at all. Another issue is what fields cause a postback. An example would be a field that can be empty (zero), but if it's >0 another field needs to be populated. The postback occurs after the first field and an error message is displayed next to the second field saying it can't be empty, yet the user hasn't even had a chance to enter anything in that field. This is most likely a simple fix of where the validation originates from (the second field instead of the first), but the programmer seems to think this is hard to do or not doable. I may have to dig into C#, but some help here would be appreciated.
Thanks,
Russell
|
|
|
|
|
Why are you validating on the server when the user tabs out? I'd keep the "light" validation on the client side with JavaScript and save the postbacks for submitting the form. Then you can set the focus to the field that has errors, or move them on.
That validation you are mentioning should definitely be clientside. Is the developer rolling their own code, or using something supplied by the framework like a RequiredFieldValidator that can be set to dynamic and will run on the clientside without a postback?
If there is some compelling reason why you would roundtrip every time a user leaves a field (which seems to make sense only for a small internal app without many concurrent users, otherwise it won't scale well) then I'd look at using a callback instead of a postback for the validation. They are not complicated to wire in and infinitely improve the user experience while cutting down on the size of network traffic (have you used Fiddler or something similar to see how much data, like ViewState, gets thrown around when you postback?)
|
|
|
|
|
I have been thinking clientside validation is the way to go, also. I'm coming in late to this project and we have a deadline looming on 9/1, so that's a consideration when making changes. I only realized we had this problem yesterday, unfortunately. The page is for external users, but the volume will be low (about 1600 users entering one order each over the period of two - three months). As you can see, the amount of traffic is not really an issue, but serverside validation may not be the way to go due to these other issues. Thanks for your reply.
|
|
|
|
|
Checking for blank fields, etc should definetly be done on the client side.
Here is an example Javascript validation routine:
function valToFormulator() {
if ((document.getElementById('<%=tbxCustomerID.ClientID%>').value == "") ||
(document.getElementById('<%=tbxContactID.ClientID%>').value == "")) {
alert("You must select a contact before sending to the flavor chemist.");
return false;
}
else
return true;
}
On the ASP.NET button, I have the following attribute:
OnClientClick="return valToFormulator();"
If the validation routine returns a "false", then the PostBack does not occur and the validation routine displays the Popup alert box with the error message.
Have your programmer try it out on a simple field validation where a field cannot be blank. You will be much happier with the performance.
|
|
|
|
|
Hello
My web page has download and upload page. I design the form with text boxes and combo boxes. I want to know what should I write in the code-behind for the button to collect the information from the text boxes and recieve the attached file.
Excuse me for bad english writing.
|
|
|
|
|
If all the controls are Runat=Server
you can directly access the objects in the server side. Like
fileupload.SaveAs("filelocation") //File Upload is asp:FileUpload control
YourTextbox.Text = "Hello"
Hope this helps.
|
|
|
|
|
hasani2007 wrote: I design the form with text boxes and combo boxes
What is the purpose of TextBox and Combo Box over here ?
Use only FileUpload Control To Upload File and Use an Button say Upload. OnClick on that button upload the file.
Use this samle code. Please Check the syntax.
If(FileUplaod1.HasFile)
{
FileUpload1.SaveAs(FilePath);
}
Abhijit Jana | Codeproject MVP
Web Site : abhijitjana.net
Don't forget to click "Good Answer" on the post(s) that helped you.
|
|
|
|
|
|
Hello,
I am having problem in running query for the following code:
Actually My database in in MS Access and I need to compare the date column (which is in Date format in the database) with the value of the Date textbox (which is a string). So maybe I need to convert the string to date format and then compare......should I convert to date format?
My date is stored in the database as dd-mmm-yy. For example it's stored as 31-Aug-09.
Please assist:
public void executePositiveListQuery_ANY(string selected_Date)
{
string sql;
if(selected_Date=="empty")
sql= "SELECT DISTINCT re_MLNO FROM ml_hiv_status WHERE (re_HIV1_Status=1 AND re_HIV2_Status=1)";
else
{
string selectedDate = (Convert.ToDateTime(selected_Date)).ToShortDateStr ing();
sql= "SELECT re_MLNO FROM ml_hiv_status WHERE ( re_SpecimenDate='" + selectedDate + "' AND re_HIV1_Status=1 AND re_HIV2_Status=1)";
resultsLabel.Text ="You Have selected"+ selectedDate;
}
try
{
OleDbConnection connection = new OleDbConnection(HIV.Database.DataConstants.CONNECT ION_STRING);
OleDbDataAdapter adapter = new OleDbDataAdapter();
OleDbCommand command = new OleDbCommand(sql, connection);
DataSet ds = new DataSet();
adapter.SelectCommand = command;
int count = adapter.Fill(ds);
if (count > 0 && count <= 200)
{
DataView view = ds.Tables[0].DefaultView;
resultsDatagrid.DataSource = view;
resultsDatagrid.DataBind();
resultsDatagrid.Visible = true;
resultsLabel.Text = count.ToString() + " results found.";
resultsLabel.Visible = true;
exportLinkbutton.Visible = true;
}
else if (count> 200)
{
DataView view = ds.Tables[0].DefaultView;
resultsDatagrid.DataSource = view;
resultsDatagrid.DataBind();
resultsDatagrid.Visible = false;
resultsLabel.Text = "More than 200 results found. Please Click the Export to Excel Link to Download the Results.";
resultsLabel.Visible = true;
exportLinkbutton.Visible = true;
}
else
{
resultsLabel.Text = "No results found.";
resultsLabel.Visible = true;
exportLinkbutton.Visible = false;
resultsDatagrid.Visible = false;
}
connection.Close();
}
catch(Exception ex)
{
message.InnerHtml = "SQL: " + sql + "<p></p>" + ex.ToString(); here!!
}
|
|
|
|
|
The best thing to do would be to parameterise the query so the conversion work is done for you.
Failing that, converting the date format in your query string to either yyyy-mm-dd or dd mmm yyyy should do the trick.
|
|
|
|
|
How would I do that? Can you please help?
|
|
|
|