Showing posts with label time. Show all posts
Showing posts with label time. Show all posts

Monday, March 26, 2012

pass value to image control

This is my first time to use asp.net. I just want to get an image name passed from url (by using request.querystring), and then display this image, when user click the image, I also display the mouse coordinate. But I just can not get it to work, please help. Thanks. The following is my code:

<script runat="server">
Sub getCoordinates(sender As Object, e As ImageClickEventArgs)
mess.Text="Coordinates: " & e.x & ", " & e.y
End Sub
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim imagename as String = Request.QueryString("name")
Dim imagePath as String =Server.MapPath("/keller/portrait/" & imagename)
End Sub
</script>
<html>
<body>
<form runat="server">
<p>Click on the image:</p>
<asp:Image
runat="server"
ImageUrl="<%=iamgePath%>"
OnClick="getCoordinates"/>
<p><asp:label id="mess" runat="server"/></p>
</form>
</body>
</html>
Welcome to the ASP.NET Forums, lily1.

The following update to your code will correctly show the image passed in with the QueryString.

<%@. page language="VB" debug="true" %>
<html>
<head>
<title>This is main Page</title>
<script runat="server"
Sub getCoordinates(sender As Object, e As ImageClickEventArgs)
mess.Text="Coordinates: " & e.x & ", " & e.y
End Sub

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim imagename as String = Request.QueryString("name")
Dim imagePath As String = Server.MapPath("/keller/portrait/" & imagename & ".gif")
MyImage.ImageUrl = imagePath
End Sub

</script>
<html>
<body>
<form runat="server">
<p>Click on the image:</p>
<asp:Image runat="server"
id="MyImage"
OnClick="getCoordinates" />
<p><asp:label id="mess" runat="server" /></p>
</form>
</body>
</html>

The only difference is that I gave the asp:Image control an ID, and used this ID in your Page_Load code to set its ImageUrl property.

For details of how to get your imagemap working, see the following tutorial:
ImageMap.NET

I hope this helps.
Thanks for your help. I tried your script, The picture can be displayed correctly now, but when i click the picture, I got javascript error, it seems that function 'getCoordinates' does not execute at server instead it tries to run at client side. Here is url of the page:http://user1046822.wx15.registeredsite.com/keller/imagecrop_coodinates.aspx?name=test.jpg . Could you please tell me what is the problem? Thanks again.
> Could you please tell me what is the problem?

You've defined the problem yourself ... that getCoordinates tries to run at the client-side, not at the server.

Please go through the article I provided above. That will explain how to send click coordinates back to your server-side code.

Wednesday, March 21, 2012

Passing a string[] into a class

hi i am trying to pass a string array( all of the values at the same time) into a class

I am using this code trying to pass into this class

string[] itemTitle;string[] itemNames; OdbcConnection userCheckCon = sqlconnect.Connect(); OdbcCommand userCheck =new OdbcCommand("select Username from tbl_usr where Username = '" + uName +"'", userCheckCon); OdbcDataReader userRead = userCheck.ExecuteReader();int count = 0;while (userRead.Read()) {if (userRead.HasRows && variablePass) { itemTitle[count] ="userName"; itemNames[count] = uName; }else { itemTitle[count] ="userName"; itemNames[count] = ("<span class='greenHigh'>" + uName +" is available </span>"); } count++; } userRead.Close(); userCheckCon.Close();//END XML xmlGen userC =new xmlGen(); userC.xmlStart(writer); userC.addNode(writer,itemTitle[],itemNames[]); userC.xmlEnd(writer);
 
The problem is on the userC.addNode line and this is the class i am trying to pass into it
class 
 public XmlTextWriter addNode(XmlTextWriter writer,string[] itemTitle,string[] itemName)
{
for(int items = 0; items < itemTitle.Length; items++)
{
writer.WriteElementString(itemTitle[items], itemName[items]);
}
return writer;
}
 
Thanks
Dan 

Two problems.


1. Your string[] itemTitle needs a = new string[MaxRows] where maxrows is an int. If you dont know the size until later in the code you can always use a List<string> instead.

2 (and more importantly), take out the []'s from userC.addNode(writer,itemTitle[],itemNames[]); Should be userC.addNode(writer,itemTitle,itemNames);


hi

using arry of strings here is not recomended ,

because we dont know the number of elements ,

you must useStringCollection class ,

this is a dynamic array,

you can add and remove from it as you want ,

so change you variables types as follows :

StringCollection itemTitle=new StringCollection ();

StringCollection itemNames=new StringCollection ();

now to add items to these colletions use this :

itemTitle.add("your string") ....

and to iterate inside these collections use this :

for( int i=0; collection.count-1; i++)

{ yoou code here }

regards,


This code is also vulnerable to SQL injection.


Hi thanks will give this all a go tonight

and how is it vunerable to SQL injection


 OdbcCommand userCheck =new OdbcCommand("select Username from tbl_usr where Username = '" + uName +"'", userCheckCon);
 
 
if uName is like the query string or textbox text value all I need to enter to return the all rows isanything' OR 'x'='x
 
Or If I want to get more malicious I can do a ' delete from Users --
 
Look into stored procedures if you want to solve this problem.