Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with GridView

Status
Not open for further replies.

pwomack

Technical User
Apr 16, 2005
121
US
New to asp.net and using Visual Studio 2008.

I have a GridView with a column named "Doc" that stores a UNC path for a document. I don't want this column visible, so I set the visible property to "False".

I added a templete field to the GridView with a button that the user will click. I managed to get the Button_click event to fire where I get the value of the "Doc" column for the current row.

However, the Button1_Click subroutine only gets the value of the "Doc" row when the "Doc" column is visible. When I set the "Doc" column visible property to "False" I don't get the UNC path.

Can someone help with this?

Here is the "Doc" field that contains the UNC path.

Code:
            <asp:BoundField DataField="Doc" HeaderText="Doc" 
                SortExpression="Doc" Visible="False" >
                <ControlStyle Font-Size="Smaller" Height="1px" Width="1px" />
                <FooterStyle Font-Size="Smaller" Height="1px" Width="1px" />
                <HeaderStyle Font-Size="Smaller" Height="1px" BackColor="#80FFFF" Width="1px" />
                <ItemStyle Font-Size="Smaller" Height="1px" Width="1px" />
            </asp:BoundField>

Here is the templete field with the button.

Code:
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Button ID="Button1" Runat="Server" OnClick="Button1_Click" Text="View Document" />
                </ItemTemplate>
                <ControlStyle Font-Size="Smaller" />
                <HeaderStyle BackColor="#80FFFF" />
                <ItemStyle Font-Size="Smaller" />
            </asp:TemplateField>

Here is the code behind for the button click event. The strField1 variable = "" when the "Doc" column visible property is set to false. When the "Doc" column visible property is set to true, then the strField1 variable = "\\*****-***\d$\TrainingDocs\Project Management for Project Team Members_One day.doc"

Code:
    Protected Sub Button1_Click(ByVal o As Object, ByVal e As EventArgs)

        Dim Button1 As Button = DirectCast(o, Button)
        Dim grdRow As GridViewRow = DirectCast(Button1.Parent.Parent, GridViewRow)
        Dim strField1 As String = grdRow.Cells(0).Text

    End Sub
 
I would go about this slightly different.
1. put the UNC doc value in the gridview's DataKeys collection
2. use the OnRowCommand event of the gridview
3. Give the button in your template a command name like "viewdoc"
4. pull the UNC doc value from the datakeys in the OnRowCommand.

something like this
Code:
<asp:gridview ... DataKeys = "id,doc" OnRowCommand="do_specified_action">
<columns>
   <itemtemplate>
       <asp:button CommandName="view document" .../>
   </itemtemplate>
</columns>
</asp:gridview>
Code:
MyGridview.DataSource = GetData();
MyGridview.DataBind();
Code:
public void do_specified_action(object sender, GridVieRowCommandEventArgs e)
{
   if(e.CommandName == "view document")
   {
      var document = MyGridView.DataKeys[e.RowIndex]["doc"];
      //do something with the object doucment
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I would do it the way Jason suggests. However, I have run into this problem with the gridview as well. It works differently from the 1.1 DataGrid.
If you set the visible property in the property windows to False, then you cannot get the value. What you have to do is to set the visible property to false in the code behind. Then you can reference the value.

Something like this in the RowCreated Event
Code:
   e.Row.Cells(-x-).Visible = False
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top