DataGrid question

  • Thread starter Thread starter brock wade
  • Start date Start date
B

brock wade

I have a Datagrid that is working fine displying my records, but I'm
trying to program buttons
on each record line to launch another web page that shows all the
details for the product:

<asp:datagrid id="dgProducts" runat="server">
<asp:ButtonColumn Text="Details" CommandName="Details"
ButtonType="PushButton"></asp:ButtonColumn>
</asp:datagrid>

Should I go to my code-behind to do something like this?:

Sub detailsClicked(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
Response.Redirect("www.mysite.com\details.aspx")
End Sub

If so how can I pass the chosen record's key information into the new
page to pickup the details?
 
You can do it that way and bind the key you need to display details to the
EventArgument.
you can just add a hyperlink to the datagrind and bind the key to the url
similar to
above.
Or if you want to get creative you can set the post back url on the button
to your detail page. just be careful of circular references. (i can give you
an artical if you want to know how to do that)
 
I have a Datagrid that is working fine displying my records, but I'm
trying to program buttons
on each record line to launch another web page that shows all the
details for the product:

<asp:datagrid id="dgProducts" runat="server">
<asp:ButtonColumn Text="Details" CommandName="Details"
ButtonType="PushButton"></asp:ButtonColumn>
</asp:datagrid>

Should I go to my code-behind to do something like this?:

Sub detailsClicked(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
Response.Redirect("www.mysite.com\details.aspx")
End Sub

If so how can I pass the chosen record's key information into the new
page to pickup the details?

Brock

In the Itemdatabound event you'll want to set the button's
commandargument to the chosen record's key.
Also set the button's commandname to something like "redirect" or
something that associates that commandname with the event you're
firing.

You're going to capture the button's click event in the OnItemCommand
event of the datagrid. Make a simple select case statement that
checks the command name's property of the button - inside that
statement you'll check the commandarguement to get the value.

here's an example:

protected void ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType== ListItemType.Item || e.Item.ItemType==
ListItemType.AlternatingItem)
{
DataRowView drv = (DataRowView)e.Item.DataItem;
ImageButton btn =
(ImageButton)e.Item.FindControl("myImageButton");
btn.CommandName = "MyCommandName";
btn.CommandArguement = drv["ID"].ToString();
}
}

then the itemcommand will look like this:

protected void ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
switch(e.CommandName)
{
default:
break;
case "MyCommandName":
Response.Redirect("www.mysite.com\" +
e.CommandArguement)
break;
}
}
 
Forgot to mention, I'm using VB.net.

I have a Datagrid that is working fine displying my records, but I'm
trying to program buttons
on each record line to launch another web page that shows all the
details for the product:
<asp:datagrid id="dgProducts" runat="server">
        <asp:ButtonColumn Text="Details" CommandName="Details"
ButtonType="PushButton"></asp:ButtonColumn>
        </asp:datagrid>
Should I go to my code-behind to do something like this?:
    Sub detailsClicked(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
        Response.Redirect("www.mysite.com\details.aspx")
    End Sub
If so how can I pass the chosen record's key information into the new
page to pickup the details?

Brock

In the Itemdatabound event you'll want to set the button's
commandargument to the chosen record's key.
Also set the button's commandname to something like "redirect" or
something that associates that commandname with the event you're
firing.

You're going to capture the button's click event in the OnItemCommand
event of the datagrid.  Make a simple select case statement that
checks the command name's property of the button - inside that
statement you'll check the commandarguement to get the value.

here's an example:

protected void ItemDataBound(object sender, DataGridItemEventArgs e)
{
     if (e.Item.ItemType== ListItemType.Item || e.Item.ItemType==
ListItemType.AlternatingItem)
     {
         DataRowView drv = (DataRowView)e.Item.DataItem;
         ImageButton btn =
(ImageButton)e.Item.FindControl("myImageButton");
         btn.CommandName = "MyCommandName";
         btn.CommandArguement = drv["ID"].ToString();
     }

}

then the itemcommand will look like this:

protected void ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
     switch(e.CommandName)
     {
         default:
              break;
         case "MyCommandName":
              Response.Redirect("www.mysite.com\" +
e.CommandArguement)
              break;
     }



}- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -
 
Forgot to mention, I'm using VB.net.

In the Itemdatabound event you'll want to set the button's
commandargument to the chosen record's key.
Also set the button's commandname to something like "redirect" or
something that associates that commandname with the event you're
firing.
You're going to capture the button's click event in the OnItemCommand
event of the datagrid.  Make a simple select case statement that
checks the command name's property of the button - inside that
statement you'll check the commandarguement to get the value.
here's an example:
protected void ItemDataBound(object sender, DataGridItemEventArgs e)
{
     if (e.Item.ItemType== ListItemType.Item || e.Item.ItemType==
ListItemType.AlternatingItem)
     {
         DataRowView drv = (DataRowView)e.Item.DataItem;
         ImageButton btn =
(ImageButton)e.Item.FindControl("myImageButton");
         btn.CommandName = "MyCommandName";
         btn.CommandArguement = drv["ID"].ToString();
     }

then the itemcommand will look like this:
protected void ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
     switch(e.CommandName)
     {
         default:
              break;
         case "MyCommandName":
              Response.Redirect("www.mysite.com\" +
e.CommandArguement)
              break;
     }
}- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -

heh well I could translate it to vb.net for you but honestly there
isnt much difference. The concepts are exactly the same - the
difference is merely syntactical.

If you're having a problem deciphering c# you can just copy and paste
it into a c#/vb.net translator - there's a handful of them on the web.
 
if it's a simple key you should be able to bind it directly without going to
the event.
Use the designer and bind the key by selecting the button and selection the
data item you want.
--
Share The Knowledge. I need all the help I can get and so do you!


the warlock society said:
I have a Datagrid that is working fine displying my records, but I'm
trying to program buttons
on each record line to launch another web page that shows all the
details for the product:

<asp:datagrid id="dgProducts" runat="server">
<asp:ButtonColumn Text="Details" CommandName="Details"
ButtonType="PushButton"></asp:ButtonColumn>
</asp:datagrid>

Should I go to my code-behind to do something like this?:

Sub detailsClicked(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
Response.Redirect("www.mysite.com\details.aspx")
End Sub

If so how can I pass the chosen record's key information into the new
page to pickup the details?

Brock

In the Itemdatabound event you'll want to set the button's
commandargument to the chosen record's key.
Also set the button's commandname to something like "redirect" or
something that associates that commandname with the event you're
firing.

You're going to capture the button's click event in the OnItemCommand
event of the datagrid. Make a simple select case statement that
checks the command name's property of the button - inside that
statement you'll check the commandarguement to get the value.

here's an example:

protected void ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType== ListItemType.Item || e.Item.ItemType==
ListItemType.AlternatingItem)
{
DataRowView drv = (DataRowView)e.Item.DataItem;
ImageButton btn =
(ImageButton)e.Item.FindControl("myImageButton");
btn.CommandName = "MyCommandName";
btn.CommandArguement = drv["ID"].ToString();
}
}

then the itemcommand will look like this:

protected void ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
switch(e.CommandName)
{
default:
break;
case "MyCommandName":
Response.Redirect("www.mysite.com\" +
e.CommandArguement)
break;
}
}
 
Thanks for the translator tip - I'm a newbi in ASP.net so I've got a
lot to learn. I got the basic translation to place into my code-
behind. I do have a few questions to make sure I'm following the logic
here. For one thing the compiler does not like the "e.CommandArgument"
even though Intellisense lists it as a choice "Option Strict On
disallows implicit conversions from 'System.Object' to Boolean'. I'm
assuming the "Response.Redirect("c:\inetpub\wwwroot\HR_ReportingTool
\HR_ReportingToolClient_1\Details.aspx"" +", e.CommandArgument)" is
basically correct for the redirect since my application itself resides
at "c:\inetpub\wwwroot\HR_ReportingTool\HR_ReportingToolClient_1".
Plus I hope I'm referencing the Button name correctly between the code-
behind and the html as "Details":

' ****** PARTIAL SAMPLE OF THE
CODE-BEHIND ******
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
lblWelcome.Text = "Hello " & Global.UserSecurity.Fname.Trim &
" " & Global.UserSecurity.Lname
Dim act As Action
Dim pos As Position
Dim empname As String
Dim lvi As ListItem
Dim Employee As Employee
Dim empcount As Integer
act = (New
ActionBroker).GetActionCurrent(Global.UserSecurity.EmpId, Today,
Global.UserName, Global.UserPassword, Global.appDataSource)
pos = (New PositionBroker).GetPosition(act.PositionID,
Global.UserName, Global.UserPassword, Global.appDataSource)
m_department = pos.Department.Name
Dim emps As Employees = (New
EmployeeBroker).GetCurrentEmployeesByDepartment(m_department,
Global.UserName, Global.UserPassword, Global.appDataSource)
Dim dt As New DataTable
Dim count As Integer = 0
For Each emp As Employee In emps
SetListViewItem(emp, dt, count)
count = count + 1
Next
dgEmployees.DataSource = dt
dgEmployees.DataBind()

End Sub

Private Sub SetListViewItem(ByVal dr As Employee, ByVal dt As
DataTable, ByVal count As Integer)
If count = 0 Then
dt.Columns.Add("Emp #")
dt.Columns.Add("Last Name")
dt.Columns.Add("First Name")
dt.Columns.Add("Title")
End If
Dim EmpPos As Action = (New
ActionBroker).GetActionCurrent(dr.Key, Today, Global.UserName,
Global.UserPassword, Global.appDataSource)
Dim employee As DataRow = dt.NewRow
employee("Emp #") = dr.Key
employee("Last Name") = dr.LastName
employee("First Name") = dr.FirstName
employee("Title") = EmpPos.WorkAgainstInfo.Title
dt.Rows.Add(employee)
End Sub 'SetListViewItem

Protected Sub ItemDataBound(ByVal sender As Object, ByVal e As
DataGridItemEventArgs)
If ((e.Item.ItemType = ListItemType.Item) _
OrElse (e.Item.ItemType =
ListItemType.AlternatingItem)) Then
Dim drv As DataRowView = CType(e.Item.DataItem,
DataRowView)
Dim btn As ImageButton =
CType(e.Item.FindControl("Details"), ImageButton)
btn.CommandName = "Details"
btn.CommandArgument = drv("Key").ToString
End If
End Sub

Protected Sub OnItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs)
Select Case (e.CommandName)
Case "Details"
Response.Redirect("c:\inetpub\wwwroot\HR_ReportingTool
\HR_ReportingToolClient_1\Details.aspx"" +", e.CommandArgument)
End Select
End Sub
' ****** END PARTIAL SAMPLE OF
THE CODE-BEHIND ******

' ****** HTML ******

<asp:datagrid id="dgEmployees" runat="server" lowSorting="True"
<Columns>
<asp:ButtonColumn Text="Details" CommandName="Details"
ButtonType="PushButton"></asp:ButtonColumn>
</Columns>
</asp:datagrid>

Of course I am a ways away from creating the Employees Detail .aspx
and about 20 text boxes hopefully to be filled with the balance of the
Employee fields.

Thanks!!!! Brockus








Forgot to mention, I'm using VB.net.
I have a Datagrid that is working fine displying my records, but I'm
trying to program buttons
on each record line to launch another web page that shows all the
details for the product:
<asp:datagrid id="dgProducts" runat="server">
        <asp:ButtonColumn Text="Details" CommandName="Details"
ButtonType="PushButton"></asp:ButtonColumn>
        </asp:datagrid>
Should I go to my code-behind to do something like this?:
    Sub detailsClicked(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
        Response.Redirect("www.mysite.com\details.aspx")
    End Sub
If so how can I pass the chosen record's key information into the new
page to pickup the details?
Brock
In the Itemdatabound event you'll want to set the button's
commandargument to the chosen record's key.
Also set the button's commandname to something like "redirect" or
something that associates that commandname with the event you're
firing.
You're going to capture the button's click event in the OnItemCommand
event of the datagrid.  Make a simple select case statement that
checks the command name's property of the button - inside that
statement you'll check the commandarguement to get the value.
here's an example:
protected void ItemDataBound(object sender, DataGridItemEventArgs e)
{
     if (e.Item.ItemType== ListItemType.Item || e.Item.ItemType==
ListItemType.AlternatingItem)
     {
         DataRowView drv = (DataRowView)e.Item.DataItem;
         ImageButton btn =
(ImageButton)e.Item.FindControl("myImageButton");
         btn.CommandName = "MyCommandName";
         btn.CommandArguement = drv["ID"].ToString();
     }
}
then the itemcommand will look like this:
protected void ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
     switch(e.CommandName)
     {
         default:
              break;
         case "MyCommandName":
              Response.Redirect("www.mysite.com\" +
e.CommandArguement)
              break;
     }
}- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -

heh well I could translate it to vb.net for you but honestly there
isnt much difference.  The concepts are exactly the same - the
difference is merely syntactical.

If you're having a problem deciphering c# you can just copy and paste
it into a c#/vb.net translator - there's a handful of them on the web.- Hide quoted text -

- Show quoted text -
 
Thanks for the translator tip - I'm a newbi in ASP.net so I've got a
lot to learn. I got the basic translation to place into my code-
behind. I do have a few questions to make sure I'm following the
logic
here. For one thing the compiler does not like the
"e.CommandArgument"
even though Intellisense lists it as a choice "Option Strict On
disallows implicit conversions from 'System.Object' to Boolean'. I'm
assuming the "Response.Redirect("c:\inetpub\wwwroot\HR_ReportingTool
\HR_ReportingToolClient_1\Details.aspx"" +", e.CommandArgument)" is
basically correct for the redirect since my application itself
resides
at "c:\inetpub\wwwroot\HR_ReportingTool\HR_ReportingToolClient_1".
Plus I hope I'm referencing the Button name correctly between the
code-
behind and the html as "Details":

' ****** PARTIAL SAMPLE OF THE
CODE-BEHIND ******
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
lblWelcome.Text = "Hello " & Global.UserSecurity.Fname.Trim &
" " & Global.UserSecurity.Lname
Dim act As Action
Dim pos As Position
Dim empname As String
Dim lvi As ListItem
Dim Employee As Employee
Dim empcount As Integer
act = (New
ActionBroker).GetActionCurrent(Global.UserSecurity.EmpId, Today,
Global.UserName, Global.UserPassword, Global.appDataSource)
pos = (New PositionBroker).GetPosition(act.PositionID,
Global.UserName, Global.UserPassword, Global.appDataSource)
m_department = pos.Department.Name
Dim emps As Employees = (New
EmployeeBroker).GetCurrentEmployeesByDepartment(m_department,
Global.UserName, Global.UserPassword, Global.appDataSource)
Dim dt As New DataTable
Dim count As Integer = 0
For Each emp As Employee In emps
SetListViewItem(emp, dt, count)
count = count + 1
Next
dgEmployees.DataSource = dt
dgEmployees.DataBind()


End Sub


Private Sub SetListViewItem(ByVal dr As Employee, ByVal dt As
DataTable, ByVal count As Integer)
If count = 0 Then
dt.Columns.Add("Emp #")
dt.Columns.Add("Last Name")
dt.Columns.Add("First Name")
dt.Columns.Add("Title")
End If
Dim EmpPos As Action = (New
ActionBroker).GetActionCurrent(dr.Key, Today, Global.UserName,
Global.UserPassword, Global.appDataSource)
Dim employee As DataRow = dt.NewRow
employee("Emp #") = dr.Key
employee("Last Name") = dr.LastName
employee("First Name") = dr.FirstName
employee("Title") = EmpPos.WorkAgainstInfo.Title
dt.Rows.Add(employee)
End Sub 'SetListViewItem


Protected Sub ItemDataBound(ByVal sender As Object, ByVal e As
DataGridItemEventArgs)
If ((e.Item.ItemType = ListItemType.Item) _
OrElse (e.Item.ItemType =
ListItemType.AlternatingItem)) Then
Dim drv As DataRowView = CType(e.Item.DataItem,
DataRowView)
Dim btn As ImageButton =
CType(e.Item.FindControl("Details"), ImageButton)
btn.CommandName = "Details"
btn.CommandArgument = drv("Key").ToString
End If
End Sub


Protected Sub OnItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs)
Select Case (e.CommandName)
Case "Details"
Response.Redirect("c:\inetpub\wwwroot
\HR_ReportingTool
\HR_ReportingToolClient_1\Details.aspx"" +", e.CommandArgument)
End Select
End Sub
' ****** END PARTIAL SAMPLE OF
THE CODE-BEHIND ******


' ****** HTML ******


<asp:datagrid id="dgEmployees" runat="server" lowSorting="True"
<Columns>
<asp:ButtonColumn Text="Details"
CommandName="Details"
ButtonType="PushButton"></asp:ButtonColumn>
</Columns>
</asp:datagrid>


Of course I am a ways away from creating the Employees Detail .aspx
and about 20 text boxes hopefully to be filled with the balance of
the
Employee fields.


Thanks!!!! Brockus




Forgot to mention, I'm using VB.net.
I have a Datagrid that is working fine displying my records, but I'm
trying to program buttons
on each record line to launch another web page that shows all the
details for the product:
<asp:datagrid id="dgProducts" runat="server">
        <asp:ButtonColumn Text="Details" CommandName="Details"
ButtonType="PushButton"></asp:ButtonColumn>
        </asp:datagrid>
Should I go to my code-behind to do something like this?:
    Sub detailsClicked(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
        Response.Redirect("www.mysite.com\details.aspx")
    End Sub
If so how can I pass the chosen record's key information into the new
page to pickup the details?
Brock
In the Itemdatabound event you'll want to set the button's
commandargument to the chosen record's key.
Also set the button's commandname to something like "redirect" or
something that associates that commandname with the event you're
firing.
You're going to capture the button's click event in the OnItemCommand
event of the datagrid.  Make a simple select case statement that
checks the command name's property of the button - inside that
statement you'll check the commandarguement to get the value.
here's an example:
protected void ItemDataBound(object sender, DataGridItemEventArgs e)
{
     if (e.Item.ItemType== ListItemType.Item || e.Item.ItemType==
ListItemType.AlternatingItem)
     {
         DataRowView drv = (DataRowView)e.Item.DataItem;
         ImageButton btn =
(ImageButton)e.Item.FindControl("myImageButton");
         btn.CommandName = "MyCommandName";
         btn.CommandArguement = drv["ID"].ToString();
     }
}
then the itemcommand will look like this:
protected void ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
     switch(e.CommandName)
     {
         default:
              break;
         case "MyCommandName":
              Response.Redirect("www.mysite.com\" +
e.CommandArguement)
              break;
     }
}- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -

heh well I could translate it to vb.net for you but honestly there
isnt much difference.  The concepts are exactly the same - the
difference is merely syntactical.

If you're having a problem deciphering c# you can just copy and paste
it into a c#/vb.net translator - there's a handful of them on the web.- Hide quoted text -

- Show quoted text -
 
Sould I be binding the actual DataGrid to the DataSource using "Simple
Binding" which gives me the Option of "Page"? If so or not how do I
launch then my Details.aspx and pickup the fileds on that screen which
correspond to the reocord chosen by the button specific to the
datarow?
Thanks!


if it's a simple key you should be able to bind it directly without going to
the event.
Use the designer and bind the key by selecting the button and selection the
data item you want.
--
Share The Knowledge. I need all the help I can get and so do you!



the warlock society said:
In the Itemdatabound event you'll want to set the button's
commandargument to the chosen record's key.
Also set the button's commandname to something like "redirect" or
something that associates that commandname with the event you're
firing.
You're going to capture the button's click event in the OnItemCommand
event of the datagrid.  Make a simple select case statement that
checks the command name's property of the button - inside that
statement you'll check the commandarguement to get the value.
here's an example:
protected void ItemDataBound(object sender, DataGridItemEventArgs e)
{
     if (e.Item.ItemType== ListItemType.Item || e.Item.ItemType==
ListItemType.AlternatingItem)
     {
         DataRowView drv = (DataRowView)e.Item.DataItem;
         ImageButton btn =
(ImageButton)e.Item.FindControl("myImageButton");
         btn.CommandName = "MyCommandName";
         btn.CommandArguement = drv["ID"].ToString();
     }
}
then the itemcommand will look like this:
protected void ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
     switch(e.CommandName)
     {
         default:
              break;
         case "MyCommandName":
              Response.Redirect("www.mysite.com\" +
e.CommandArguement)
              break;
     }
}- Hide quoted text -

- Show quoted text -
 
Back
Top