|
To Clarify: I need to conditionally, programmatically change the string value from 'Select * from [Table1]' to 'Select * from [Table2]'
I have tried various ASP <%#variable%> techniques without success.
I have also tried the following (In the Page_Load and a Button1_Click sub) :
Dim SqlDatasource1 As New SqlDataSource
Dim GridView1 As New GridView
Dim sSQLDataSource As String
sSQLDataSource = "Select * from [Table2]"
Dim SqlConnectionString As String
SqlConnectionString = "Data Source=(serverName);Initial Catalog=(databaseName);Integrated Security=True"
SqlDatasource1.ConnectionString = SqlConnectionString
SqlDatasource1.SelectCommandType = SqlDataSourceCommandType.Text
SqlDatasource1.SelectCommand = sSQLDataSource
SqlDatasource1.DataBind()
GridView1.DataSource = SqlDatasource1
GridView1.DataBind()
....
Any enlightenment with what I'm missing with the Bound GridView would be greatly appreciated.
|
|
|
|
|
Just in case this can help anybody else, the only solution I was able to find around the Bound GridView SelectCommand was to use a store procedure and pass parameter options via Session variables to return dynamic record sets.
|
|
|
|
|
Hi,
I'm an A level student in the UK studying computing. For my project, I have to create a website for a real company. I'm using Visual Web Developer 2008 Express Edition along with MS Access for a database.
I've come across a slight problem which I don't yet have the knowledge for and was wondering if somebody could enlighten me. You see, I have a news page, which uses a datalist which is bound to an MS Access database. This datalist uses a query to display:
- ID (Hidden)
- Heading
- Date published
The database itself also holds the article as a memo in a field. I want to implement a button, which will redirect the user to a page containing the article. My idea was to send the ID of the record from the datalist to another page, and use this information as an integer on the next page for a query, which will select the matching article (matching the article ID to the integer), and present it on the page. Here are the code snippets to help understand better:
News.aspx
<asp:datalist id="DataList1" runat="server" datakeyfield="ID" xmlns:asp="#unknown">
DataSourceID="NewsDataSource">
<itemtemplate>
ID:
<asp:label id="IDLabel" runat="server" text="<%# Eval("ID") %>" />
<br />
Heading:
<asp:label id="HeadingLabel" runat="server" text="<%# Eval("Heading") %>" />
<br />
Published on:
<asp:label id="Published_onLabel" runat="server">
Text='<%# Eval("[Published on]") %>' />
<br />
<br />
<asp:button id="GoToArticle" runat="server" onclick="Go_To_Article" text="Read More" commandargument="<%# Bind("ID") %>" />
</asp:label></itemtemplate>
</asp:datalist>
The Code-behind for News.aspx
'Event to transfer page control to NewsArticle.aspx
Protected Sub Go_To_Article(ByVal sender As Object, ByVal e As EventArgs)
Dim IDLabel_Value As Integer
IDLabel_Value = 'here is where I don't know how to select the ID field of datalist1
Response.Redirect("~/NewsArticle.aspx?IDLabel_Value = " & CStr(IDLabel_Value))
End Sub
And I don't know how to retrieve the ID value send to NewsArticle.aspx I have looked up on a method which uses get property on the first page and then Dim As Result on the second page. But even then, I don't know how to retrieve the ID field of the record from a datalist. Any help would be greatly appriciated.
Thanks!
|
|
|
|
|
If you select a record on page 1 and then need to use it in page 2, there's two common ways to do this:
1 - make page 1 and page 2 controls on the one page and let viewstate store it
2 - write a generic page to show a record based on id, and pass the id in on the URL. This second way has the advantage that people can bookmark the same page with different ids on the URL and go back to them.
You can also use cross page postback in later versions of .NET but that is really hacky.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
you can pass the id in query string[^].
Yusuf
Oh didn't you notice, analogous to square roots, they recently introduced rectangular, circular, and diamond roots to determine the size of the corresponding shapes when given the area. Luc Pattyn[^]
|
|
|
|
|
LOL - thanks, I didn't think of that.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
Thanks christian and yusuf for the help. I've never tried to post using a query; I gave it a shot but now I get the following error when clicking on the button:
System.NullReferenceException: Object reference not set to an instance of an object.
for line
Response.Redirect("~/NewsArticle.aspx?ID=" + this.IDLabel.Text);
I'm not sure, but I think it may have something to do with the fact that i've used
<asp:label id="IDLabel" runat="server" text="<%# Eval("ID") %>" xmlns:asp="#unknown" />
here (if xmlns:asp="#unknown" shows up that's really not there...it just shows up in the preview :P). So here is the overall code behind:
public partial class News : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label IDLabel;
protected System.Web.UI.WebControls.Button GoToArticle;
protected void Page_Load(object sender, EventArgs e)
{
}
public void Go_To_Article(object sender, System.EventArgs e)
{
Response.Redirect("~/NewsArticle.aspx?ID=" + this.IDLabel.Text);
}
}
I had followed the tutorial yusuf kindly linked me (and thank you for it ^_^), but i noticed in the source code, the author is using <form method="post"></form> , whereas I can't put a form in my news.aspx page because I've already implemented one in the master page. This has proven to be one hell of a coursework!
Thanks.
|
|
|
|
|
The problem is probably that IDLabel is inside a template, there's not ONE IDLabel to refer to, they are buried in the gridview. What you do is use that <%# Eval syntax to render a button or link for each row, I'd use a linkbutton if you need a button on the form. Then you build your URL with the query parameter inside the list, so the id is rendered correctly for each row.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
Arroci wrote: This has proven to be one hell of a coursework!
As long as you are willing to learn, we are here to help. don't give up.
Before implementing something, I would spend sometime understanding the basics. using query string is not that difficult at all. Try debugging your code and see what is the value of
this.IDLabel.Text
Yusuf
Oh didn't you notice, analogous to square roots, they recently introduced rectangular, circular, and diamond roots to determine the size of the corresponding shapes when given the area. Luc Pattyn[^]
|
|
|
|
|
ah! I think you and christian have found the source of the problem. Christian was right in saying the IDLabel does not have one thing to refer to. Your suggestion on debugging
this.IDLabel.Text seemed to have proved it because it's saying NullReferenceException was unhandled, which probably could be concluded that it's just not getting the ID value off the datalist.
Hehe I figured, the only way to really challenge myself is to just stretch my resources as much as possible to get information I need . But this has proven quiet trivial and my books don't cover this. Google works to some extend but it's a bit tricky to learn independant methods and then try to fuse them and translate to one language. Nethertheless I do enjoy it ^_^.
So I guess the next step really is how to render a link button in the <%# Eval("ID") tag? Any links to tutorials and suggestions are welcome ^_^.
Big thank you to christian and yusuf for helping me out. You guys rock!
|
|
|
|
|
I'm glad to see you doing all the hard work. Many people here will be willing to support you as long as you keep the good work. What people hate here is, if you simply ask for the source code.
Don't feel bad, we're all like you at some point of time. Keep reading and learning. Don't be afraid to ask any question. As long as you are willing to learn, we will entertain all of your questions.
Arroci wrote: So I guess the next step really is how to render a link button
what do you mean? I did not get it?
Yusuf
Oh didn't you notice, analogous to square roots, they recently introduced rectangular, circular, and diamond roots to determine the size of the corresponding shapes when given the area. Luc Pattyn[^]
|
|
|
|
|
something like this seems to be working now BUT it doesn't seem to pick up the Eval. I think Christian meant something like this:
<asp:linkbutton id="GoToArticle" onclick="Go_To_Article" runat="server" xmlns:asp="#unknown"><![CDATA[<%# Eval("ID") %>]]></asp:linkbutton>
Note: [xmlns:asp="#unknown">] is supposed to be [><%# Eval("ID") %>]. Don't know why it comes out messed up but there you go
Now this button works because it goes to the next page but it doesn't submit the query string however I know why and that's because I don't know how to extract the Eval("ID") from the button hehe. So the part i'm stuck on is this:
Response.Redirect("~/NewsArticle.aspx?ID=" + this.GoToArticle)
I know (well, I get the feeling) that the this.GoToArticle is supposed to be expanded more but the intellisense is confusing me haha. I have placed the Eval in between the opening tag of the link button and the closing tag of the link button. If I could extract this value (maybe as a string?) it might fix it. I have a hunch I should be able to, because you can extract values from text boxes as strings. ]]>
|
|
|
|
|
Arroci wrote: ]]>>
try Bind("ID")
Arroci wrote: Note: [xmlns:asp="#unknown">
you need either to check "Igore HTML Tag" or encode '<' & '>'
Yusuf
Oh didn't you notice, analogous to square roots, they recently introduced rectangular, circular, and diamond roots to determine the size of the corresponding shapes when given the area. Luc Pattyn[^]
|
|
|
|
|
Arroci wrote: Response.Redirect("~/NewsArticle.aspx?ID=" + this.GoToArticle)
No, I meant that you generate the link inside the databound control, and use Eval("ID") to emit the correct ID for each URL.
Arroci wrote: <asp:linkbutton id="GoToArticle" onclick="Go_To_Article" runat="server" xmlns:asp="#unknown">
Get rid of the onclick and instead add a URL that's something like ~/NewsArticle.aspx?ID=<%#Eval("ID")%>"
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
Right so I got to work with your suggestions.
Yusuf:
Since I can only use Bind("ID") within a tag, i tried it under
Text='<%# Bind("ID") %> and it just threw up the same null error. It just seems that it's not picking up the record's ID key because the button is nested in a template.
Christian
I got your suggestion to partially work. basically, it now posts to the next page, but it doesn't post the ID, instead it posts (interestingly):
http:
From the code:
<itemtemplate>
ID:
<asp:label id="IDLabel" runat="server" text="<%# Eval(" xmlns:asp="#unknown" />
<asp:linkbutton id="GoToArticle" runat="server" postbackurl="~/NewsArticle.aspx?ID=<%#Eval(" xmlns:asp="#unknown">Go</asp:linkbutton>
</itemtemplate>
So I think this is some headway. I'll also be looking up methods of posting query strings from inside an item template but I think all of us are starting to get there .
I've been searching around and came across CommandArgument codes. They seem to be practicle for putting the <![CDATA[<%# Eval("ID") %>]]> in them. I'm currently looking at a way to use this in a query string. I had tried the following:
News.aspx
<asp:linkbutton id="GoToArticle" commandname="GoToArticle" xmlns:asp="#unknown">
CommandArgument='<%#Container.DataItem, ("ID")%>' runat="server">Go</asp:linkbutton>
and the code-behind:
private void DataList1_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
{
int ID = (int)e.CommandArgument;
if (e.CommandName == "GoToArticle")
{
Response.Redirect("~/NewsArticle.aspx?ID=" + ID);
}
}
}
Ironically, even though I'm getting the error:
No Overload Method 'ToString' takes '3' arguments
for line:
<asp:linkbutton id="GoToArticle" commandname="GoToArticle" xmlns:asp="#unknown">
CommandArgument='<%#Container.DataItem, ("ID")%>' runat="server">Go</asp:linkbutton>
I may have hit something good here. I just have to figure out how to stop the overload error! any help would be appriciated .
UPDATE: Ah! Okey guys I've managed to get the query to post to the next page! The commandargument and commandname methods were correct. More importantly, I needed to add OnItemCommand to the datalist's ASP tag! If anyone requests source I would gladly post a generic copy . I think after this I might actually post an article about implementing a button in a datalist to post ID values to another page, since it required a lot of cross referencing from google to get it done .
Christian: big thank you for hinting the right direction by using the Eval to render the link button.
Yusuf: big thank you as well for directing me to posting values from one page to another using a query string.
and thank you both for aiding me through this. Now I need to get the generic page to display the article. I'm trying
<asp:label id="Label1" runat="server" xmlns:asp="#unknown"></asp:label> with
protected void Page_Load(object sender, EventArgs e)
{
this.Label1.Text = Request.QueryString["ArticleID"];
}
at the moment, but to no avail
modified on Thursday, March 12, 2009 10:33 AM
|
|
|
|
|
nevermind I've managed to get the values to pass to the other page . The next step is to use this value to match it with the ID key of a database (which has been bound to a data control on the other page) to obtain the corresponding article. Any suggestions I'm open to .
|
|
|
|
|
I have a simple question. I am trying to display a jpg on my web page. I have a help.aspx page with this line of code but it does not display. I created a directory under App_Data named JPG and the jpg image is in there. When I use the intelisense, VS can locate the jpg, but when I run the page, the image is not displayed. I have tried using just a plain html page and it does not work either. I am missing something, but what?
<asp:image id="Image1" runat="server" height="300" style="border: 1px solid black; width:auto" imageurl="~/App_Data/JPG/Monitor1.jpg" alternatetext="Main Display Page" xmlns:asp="#unknown">
Thanks
|
|
|
|
|
you forgot the magic word Abracadabra
Yusuf
Oh didn't you notice, analogous to square roots, they recently introduced rectangular, circular, and diamond roots to determine the size of the corresponding shapes when given the area. Luc Pattyn[^]
|
|
|
|
|
Because you cannot have auto with Width property. assign something i.e. 200px and it will be fine.
|
|
|
|
|
Because App_Data is a protected ASP.Net folder.
Instead place your jpg file in the any other folder which is not a ASP.Net protected folder like (images folder under the root) and try.
Regards,
Cybernate
|
|
|
|
|
I want to create a web page where the left navigation bar and the content page can be changed by the end user.
I started by creating a master page. While I can get the main content page to show, I cannot get the left hand nav bar to show it's content. In the mater page I created a conten place holder for the main content and one for the left hand nav bar and in my LeftNav.aspx I put in the code to create a list. However when I run the page nothing shows up in the left content place holder.
Has anyone done something like this and if so can you comment on this?
Thanks
Tom Wright
tawright915@gmail.com
|
|
|
|
|
Tom Wright wrote: content page can be changed by the end user.
what do you mean by this
Tom Wright wrote: I cannot get the left hand nav bar to show it's content.
where is the content coming?
Yusuf
Oh didn't you notice, analogous to square roots, they recently introduced rectangular, circular, and diamond roots to determine the size of the corresponding shapes when given the area. Luc Pattyn[^]
|
|
|
|
|
We have a content server where the user checks in a document. I grab the document from the content server and in the navbar content place holder use an include to mash in their links from the document.
Tom Wright
tawright915@gmail.com
|
|
|
|
|
Can you post the code for your master page?
Regards,
Cybernate
|
|
|
|
|
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="PolicySearch.master.cs" Inherits="ScholarPoliciesSearch.PolicySearch" %><br />
<br />
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br />
<html xmlns="http://www.w3.org/1999/xhtml" ><br />
<head runat="server"><br />
<title>Untitled Page</title><br />
<asp:ContentPlaceHolder ID="head" runat="server"><br />
</asp:ContentPlaceHolder><br />
<link href="Styles/Stylesheet.css" rel="stylesheet" type="text/css" /><br />
</head><br />
<body><br />
<form id="form1" runat="server" action="javascript:fxSearch();"><br />
<br />
<div id="HeaderContent"><br />
<br />
</div><br />
<br />
<div id="mainContent"><br />
<asp:ContentPlaceHolder ID="MainContent" runat="server"><br />
<br />
</asp:ContentPlaceHolder><br />
</div><br />
<br />
<div id="LeftNavBar"><br />
<asp:ContentPlaceHolder ID="leftNavBar" runat="server"><br />
</asp:ContentPlaceHolder><br />
</div><br />
<br />
<div id="FooterContent"><br />
<br />
</div> <br />
</form><br />
</body><br />
</html>
Tom Wright
tawright915@gmail.com
|
|
|
|