Forums: Back End:

 

ASP.NET (vb) Interfaces

first
 

JimmyTheGent ASP.NET (vb) Interfaces

I am still having a little trouble working out what role interfaces play in encapsulation etc, so I tend to just do what I need to and try to understand it later.

I have come across a problem, I am going to have a searchbox which does a database query, this will be on the master page. So I have created the following interface and the other code is on the code_behind page of the master page.

the searchbox is named txtSearch.text, when a user inputs a word and presses a submit button, I need the word to be carried across to the next page and it will then be used to query a database and populate a label. I will add the onclick event with a response.redirect later but need the class and interface working.

Something must be wrong on my code as it doesnt work, also how would I make txtSearch.text be assigned to sKeyword?

Cheers for the help.



'iSearch Interface

Imports Microsoft.VisualBasic
Public Interface ISearch
Property sKeyword() As String
End Interface






'Code_Behind

Partial Class main
Inherits System.Web.UI.MasterPage
Implements ISearch

Public Property sKeyword() As String Implements ISearch.sKeyword

Get
If txtSearch.Text.Length < 1 Then
txtSearch.Text = "No Parameters"
End If
Return skeyword.ToString()
End Get
Set(ByVal value As String)
sKeyword = value
End Set
End Property

End Class



 

JimmyTheGent

Having another look at it, would either of these work...

sKeyword(txtSearch.text)

or maybe

txtSearch.Text = sKeyword()

I feel I am supposed to be passing a parameter due to the SET part of the interface


Set(ByVal value As String)
sKeyword = value
End Set

 

Hideaway

From what I see, you're use of the Interface modifier is all wrong.

Think of an Interface as a contract. All objects that implement it MUST use all its properties and methods as defined by that contract.

I think what you're trying to achieve is have both pages "implement" the same property. I would approach it through an abstract class or a base object.

If you're going to send the data to another page anyway, why are you trying to set a property of the first page?

What exactly are you trying to achieve?

I'm getting more confused as I type...

*sleeps*

I love boots. It's like midgets hugging my calves...
quote
 

JimmyTheGent

I am completely new to encapsulation so may have my wires crossed. Since the search textbox is an input it should be encapsulated yea?

I could be slightly confused on that subject, had a lot of conflicting info all through reading on web.

so how else would I encapsulate? Whats an interface used for?

 

Hideaway

An interface simply and only defines a contract for all inheriting objects. It says, "If you're going to inherit from me, you have to play by my rules and define my properties and methods." It's mainly used in distributed objects. Or objects that someone else is going to use.

My understanding and use of encapsulation is to have a set of classes (class library) to completely manage themselves. They handle all data access, data population and expose methods to manipulate that data. It may not be the true definition, but it's the definition I use.

If you have a simple search field and want to pass the value of that field to another page, I would simply URL encode the variable. About the only thing I see from your example that you might be interested in is a "Utility" class.

Something like this would suffice and I use it all the time.

This is C# and I'm not sure of the VB conversion...

Create a "utility" object that all your ASPX pages can use and add this code


using System;
using System.Web;
using System.Web.UI;

namespace Utilities
{
public static class HTTPUtility
{
public static string SearchString = "searchstr";

// used to return string values from a query string
public static string GetRequestVar(string key)
{
if (HttpContext.Current.Request.QueryString[key] == null) return string.Empty;
return (HttpContext.Current.Request.QueryString[key]);
}

// used to return integer values like ID's
public static int GetRequestVarAsInt(string key)
{
int returnval;
if (string.IsNullOrEmpty(GetRequestVar(key))) return -1;
if (int.TryParse(GetRequestVar(key), out returnval)) return returnval;
return -1;
}
}
}


THis would be for Default.aspx


using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Add your normal Page_Load code here
//...
//
}

// Here's the button code
protected void btnSearch_Click(object sender, EventArgs e)
{
if(this.txtSearch.Text.Trim() == string.Empty) this.txtSearch.Text = "No Parameters";
else
{
Response.Redirect("SomeProcessingPage.aspx?" + Utilities.HTTPUtility.SearchString + "=" + this.txtSearch.Text.Trim(), false);
}
}
}


Now, on your processing page... SomeProcessingPage.aspx


using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public class _SomeProcessingPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Add your normal Page_Load code here
//...
//
ProcessSearch();
}

private void ProcessSearch()
{
string str = Utilities.HTTPUtility.GetRequestVar(Utilities.HTTPUtility.SearchString);
if(str == string.Empty)
{
// They hit this page directly and didn't get redirected here... Do something like kick 'em out
}
else
{
//Add functionality to to process the search request
}
}
}


Is that something you're looking for?

I love boots. It's like midgets hugging my calves...
quote
 

JimmyTheGent

I think my problem was just complete confusion. smile

I completely understand it now and god knows why i got it confused in the first place. Once I got it clear in my head it became obvious.

 
first
 

Forums: Back End: ASP.NET (vb) Interfaces

 
New Post
 
You must be logged in to post