ASP.NET - write asp.net code in C#

 
Vista:

write asp.net code in C#

Publicado por Ed (6 intervenciones) el 08/02/2008 02:42:55
Hello!

I have a code in Asp.net that calls a method in the code behind:

ASP.NET Code:
<img src="<%#CheckImage() %>" />

The objective of the following code is to check if an image exists in the databse. I have created a method in the code behind(c#) that would check the image field in the database (ImageURL), if it is there, then the method should tell the ASP.net code to place the image in the browser. However, if it is not there, then the image is ignored. I just don't know how to tell the asp.net from c# to execute the code. I'm using a boolean, and I'm not sure if this is correct.

C# Code:

public bool CheckImage()
{

OleDbConnection objConn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source=c:\Louis\Louis.mdb");OleDbCommand objCmd = new OleDbCommand("select * from WhiteHouse", objConn);
objConn.Open();

OleDbDataReader objRdr = objCmd.ExecuteReader();

while (objRdr.Read())
{

if (objRdr["ImageURL"] != null)
{

//For example <Img src="<%# DataBind.Eval(Container.DataItem, "ImageURL") %>

//meaning, go to the asp code and get the image to place it in the browser

return true;
}

else

{

//meaning, don't get the image

return false;
}

}

}

I would greatly appreciate some help.

Thanks,

Ed
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:write asp.net code in C#

Publicado por marovi (23 intervenciones) el 08/02/2008 13:48:18
La verdad es que este problema siempre es simple de resolver.

La propiedad a la que llamas es la que debe escribir la url, en este caso el bool lo debes cambiar a string y retornar el path de la imagen.

Cuando estas cargando la página todavía no existen los controles, y cuando llamas a las propiedades de esta forma es cuando se está creando la página.

Saludos y disculpa que no te responda en ingles.

chavela
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:write asp.net code in C#

Publicado por Ed (6 intervenciones) el 08/02/2008 15:34:01
Marovi,

Thanks a lot for your response. Now, I tried to do the following:

string myString = null;

Then, I created a method (string)

public string ImageUrl()
{
//all database code here

while(objRdr.Read())
{
if(objRdr["ImageURL"] != null)
{
mystring = "DataBinding.Eval(Container.DataItem, "Image URL") ";
}
}
}

Now, the problem is that the compiler does not allow me to run this. What can I do?

Thanks a lot,

Eduardo
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:write asp.net code in C#

Publicado por Javier Santamaria (312 intervenciones) el 08/02/2008 16:27:47
What does the compiler say to you? Are you getting any error?

Regards
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:write asp.net code in C#

Publicado por Eduardo (6 intervenciones) el 08/02/2008 17:13:42
Well, it just not working? i cannot run the page until I create a good method.

Thanks
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:write asp.net code in C#

Publicado por x (445 intervenciones) el 12/02/2008 17:40:12
HTML:

<asp:Label ID="lbl_ImageContainer" runat="server"><%= GetImageHtml() %></asp:Label>

C#:
public string GetImageHtml()
{
string html = "";
bool imageExists = false;
...
if (imageExists)
{
html = "<img src='myimage.jpg' align='middle' border='0' />";
}
return html;
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:write asp.net code in C#

Publicado por Ed (6 intervenciones) el 12/02/2008 22:48:29
Thanks a lot ... it worked!!!
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar