Reputation: 961
I have a need to create a small window inside of a ASP.NET Web Page(aspx) which displays a list of usernames, when you click the username, I need a new broswer window (not tab) to open up to a specific size. I can handle opening the new browser window assuming whatever control I use gets me into a code behind function where I can call....
string url = "http://www.dotnetcurry.com";
ScriptManager.RegisterStartupScript(this, this.GetType(), "OpenWin", "<script>openNewWin ('" + url + "')</script>", false);
Here's the markup that's in the page for the link.
<script language="javascript" type="text/javascript">
function openNewWin(url) {
var x = window.open(url, 'mynewwin', 'width=600,height=600,toolbar=1');
x.focus();
}
</script>
1.) What controls should I be using for this problem, what does the structure look like after I get the respective usernames back from the database?
Here's the closest I've come...This code uses an ASP.NET Bulleted List which I'm trying to bind to a list of HTML links which I would like to instead not POINT anywhere, but get me to the codebehind. Instead this Code actually renders on the page as the HTML (it isn't parsed into hyperlinks..)
protected void Page_Load(object sender, EventArgs e)
{
UsersBulletedList.DataSource = theContext.GetOnlineFavorites(4);
UsersBulletedList.DataBind();
}
public IQueryable<String> GetOnlineFavorites(int theUserID)
{
List<String> theUserList = new List<String>();
IQueryable<Favorite> theListOfFavorites= this.ObjectContext.Favorites.Where(f => f.SiteUserID == theUserID);
foreach (Favorite theFavorite in theListOfFavorites)
{
string theUserName = this.ObjectContext.SiteUsers.Where(su => su.SiteUserID == theFavorite.FriendID && su.LoggedIn == true).FirstOrDefault().UserName;
yourOnlineFavorites.Add("<a href='RealTimeConversation.aspx?UserName=" + theUserName + "'>" + theUserName + "</a>");
//this needs to help me get into a codebehind method instead of linking to another page.
}
return yourOnlineFavorites.AsQueryable();
}
Upvotes: 2
Views: 878
Reputation: 21887
I would create a Repeater
on your page and bind your results from the GetOnlineFavorites
method. Inside the Repeater
, place a LinkButton
with a ItemCommand
event that adds your script to the page.
Markup:
<asp:Repeater ID="repeater" runat="server" OnItemCommand="repeater_ItemCommand">
<ItemTemplate>
<asp:LinkButton runat="server" ID="linkButton"
Text='<%# Eval("PropertyFromBindingCollection") %>'
CommandName="OpenWindow"
CommandArgument='<%# Eval("AnotherProperty") %>' />
</ItemTemplate>
</asp:Repeater>
In here, the Text
property of the LinkButton
and the CommandArgument
of the Repeater
would be set to some properties from your collection.
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
repeater.DataSource = theContext.GetOnlineFavorites(4);
repeater.DataBind();
}
}
protected void repeater_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
if(e.CommandName == "OpenWindow")
{
string arg = e.CommandArgument; // this could be the url, or a userID to get favorites, or...?
//Your open window script
}
}
Now, when the user clicks one of the LinkButtons, it will fire the ItemCommand
event for the Repeater
, check the CommandName
(set on the LinkButton), and then get the CommandArgument
set on the LinkButton
. If you set the CommandArgument
as a URL, you can use that in your OpenWin script, otherwise use whatever data you set as the argument to get the URL you want to open.
Upvotes: 2