Passing parameter from textbox

C

Cemal Karademir

Hello,

Please forgive my simple question, but i don't know the correct synatx.

From the masterpage i want to put a text in an textbox and then select an
item in the datagrid. I do know how to pass the items in the datagrid(e.g.
PLU, description, etc.) to an other (detail)page, but i don't know the
correct syntax of how to pass the textbox value to the detailpage.

I have the following code:

<asp:DataGrid id="dgrArticle" runat="server" autoGenerateColumns="False">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<a href='ArticleDetail.aspx?debtorid=<%#textbox1.text%>&
plu=<%#DataBinder.Eval(Container.DataItem,
"PLU")%>&

description=<%#DataBinder.Eval(Container.DataItem,"DESCRIPTION")%
</a>
</ItemTemplate>
</asp:TemplateColumn
</Columns>
</asp:DataGrid>

The result is now:
http://localhost:8081/ArticleDetail.aspx?debtorid=&plu=2&description=SOME_DE
SCRIPTION

But must be (if textbox1.text="12345") :
http://localhost:8081/ArticleDetail.aspx?debtorid=12345&plu=2&description=SO
ME_DESCRIPTION

Thanx, Cemal
 
J

Jared

I don't know if I understand what you're asking, but, you are trying to
include the value of textbox1.text in a client side control, right? Why
don't you try to use a server side control, and redirect the page in the
OnClick event.

<asp:linkbutton id=LinkButton1 onclick='<%#
NavToURL(DataBinder.Eval(Container.DataItem, "PLU"),
DataBinder.Eval(Container.DataItem, "Description"))%>'
runat="server">LinkButton</asp:linkbutton>

Protected Sub NavToURL(ByVal PLU As String, ByVal Description As String)
Dim URL As String = "ArticleDetail.aspx?debtroid=" _
& Server.UrlEncode(Me.TextBox1.Text) _
& "&plu=" & Server.UrlEncode(PLU) & "&description=" &
Server.UrlEncode(Description)
Response.Redirect(URL)
End Sub
 
C

Cemal Karademir

I think this is what i'm looking for, but how dows this work in an datagrid
(see examples below). I forgot to say that I don't have much experience with
ASP.NET.

By the way, what are client and server side controls and what are the
differences/when do I use them?

Thanx, Cemal
 
J

Jared

Cemal,
First, what I mean by server side and client side controls is, a server
side control is one that posts to the server and a client control doesn't.
This may not be the correct definition, it's just what I am referring to.
That is why the <a href= will not work. The value of <a href> must be either
set when the page loads or through some scripting method. While I know the
..Net server controls do just that, I don't know how to duplicate this
behavior manually. Any of the controls listed on the WebForms toolbox band
should be server side controls.

In your datagrid control I am working under the assumption that it will have
a total of two columns, the first is PLU and the second is the description.
The PLU column will host the control that posts/redirects to the details
page. It looks like you are already using template columns, which need to
remain, at least for the PLU column. The Description column does not have to
be templated. Start by editing the template for the PLU column, Under the
ItemTemplate section drop in a LinkButton control. Set its id to "lnkPLU"
and end template editing. If your description column is templated remove it
and simply add a bound column in its place. Set the Data Field to the name
of the field that houses the data for the description column; in your
example below it looks like it should be set to DESCRIPTION. Now, in the
datagrid's ItemCommand event, write the following code, you should supply
your own error handling.

Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DataGrid1.ItemCommand
Try
Dim Item As DataGridItem = CType(e.Item, DataGridItem)
Dim URL As String
With Item
URL = "ArticleDetail.aspx?debtroid=" _
& Server.UrlEncode(Me.TextBox1.Text.Trim) _
& "&plu=" &
Server.UrlEncode(CType(.Cells(0).FindControl("lnkPLU"), LinkButton).Text) &
"&description=" & Server.UrlEncode(.Cells(1).Text)
End With
Response.Redirect(URL)
Catch ex as Exception
'Some error handling code
End Try
End Sub

I'm sure there are better ways of doing this, however, I don't know any. Pay
attention to the type conversion, a template column will hold controls,
that's why we have to do a little casting to get the value, on the other
hand, we can directly get the value of a bound column using the
cells(CellIndex).Text property.

Hope this helps,
Jared
 
C

Cemal Karademir

Okay, I did evrything you said, but now i get an error message:
System.ArgumentOutOfRangeException: .......

Am i doimg something wrong? I include below the complete source for further
investigations. Please help we this?

Thanx, Cemal

<%@ Page Language="VB" Debug="TRUE" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">

Sub Page_Load
dim SQLString as string="Select * from ARTICLE"
dim Conn as OleDbConnection
dim ConnectionString as string="Provider=Microsoft.Jet.OLEDB.4.0;
Ole DB Services=-4; Data Source=C:\SmartSoft.NET\ORSDB.mdb"
dim Cmdselect as OleDbCommand
dim dtrArticle as OleDbDataReader

Conn = New OleDbConnection (ConnectionString)
Conn.Open()
CmdSelect = New OleDbCommand(SQLString, Conn)
dtrArticle = CmdSelect.ExecuteReader()

While dtrArticle.Read()
dgrArticle.DataSource=dtrArticle
dgrArticle.DataBind()
End While

dtrArticle.Close()
Conn.Close()
End Sub

Private Sub ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs)
Dim Item As DataGridItem = CType(e.Item, DataGridItem)
Dim URL As String

With Item
URL = "ArticleDetail.aspx?debtorid=" & (Me.txtDebtor.Text.Trim)
& _
"&plu=" &
Server.UrlEncode(CType(.Cells(0).FindControl("lnkPLU"), LinkButton).Text) &
_
"&description=" & Server.UrlEncode(.Cells(1).Text)
End With

Response.Redirect(URL)
End Sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
<asp:TextBox id="txtDebtor" runat="server"
Width="100px"></asp:TextBox>
<br />
<asp:DataGrid id="dgrArticle" runat="server"
OnItemCommand="ItemCommand" autoGenerateColumns="False">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton id="lnkPLU" runat="server">
<%#DataBinder.Eval(Container.DataItem,
"PLU")%>
</asp:LinkButton>
<%#DataBinder.Eval(Container.DataItem,
"DESCRIPTION")%>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</p>
</form>
</body>
</html>
 
J

Jared

Cemal,
You are close, but it looks like both you and I missed a step. The first
thing to fix is you need to add a second column, a databound column; this
column will be used for the description. The below definition should work;
this is why you were receiving the System.ArgumentOutOfRangeException, there
was only one column. Now, I forgot to tell you to bind the "Text" property
of the linkbutton to the PLU field from the datasource. You can do it a
couple of ways. If you are editing the template column you can select
lnkPLU, open the (DataBindings) editor, select "Text", choose custom binding
expression, and set the value to DataBinder.Eval(Container.DataItem, "PLU").
Alternatively, set the "Text" property of the linkbutton control as shown
below, both produce the same results, or replace your current datagrid's
definition with the one below.

<asp:DataGrid id="dgrArticle" runat="server" OnItemCommand="ItemCommand"
autoGenerateColumns="False">
<Columns>
<asp:TemplateColumn HeaderText="PLU">
<ItemTemplate>
<asp:LinkButton id="lnkPLU" runat="server"
Text='<%#DataBinder.Eval(Container.DataItem, "PLU")%>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="DESCRIPTION"
HeaderText="Description"></asp:BoundColumn>
</Columns>
</asp:DataGrid>

On a side note, you may have faster load times if you don't populate the
data on postbacks, unless there is a chance the data will change between the
posts and it will make a difference in the results.

Remember, the page load event will fire before the ItemCommand event so
when you click an item you are always rereading from the database and
filling the datagrid each time. In this case you probably don't need to do
this.

Try something like this in the page load event:

If Not IsPostBack Then
Dim Conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Ole DB
Services=-4; Data Source=C:\SmartSoft.NET\ORSDB.mdb")
Conn.Open()
If Conn.State = ConnectionState.Open Then
Try
Dim Cmdselect As New OleDbCommand("SELECT * FROM ARTICLE", Conn)
dgrArticle.DataSource = Cmdselect.ExecuteReader
dgrArticle.DataBind()
Catch ex As Exception
'Error handling here
Finally
Conn.Close()
End Try
End If
End If

Jared
 
C

Cemal Karademir

Jared,
Thank you very much for your help, it works just like i wanted.
I will now analyse the code what it does, so that i understand what is
happening.
Thanx, Cemal.
 
J

Jared

Your welcome Cemal, if you don't understand what is going on let me know and
I will try to explain.
Jared
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top