visual studio 2010 Getting value from gridview - vb.net

How do I get the value in a gridview when one hits the "Select" link in a row?
If I have a databound, I can get that value. However I am trying to highlight certain labels.The "HighlightTex" function lets me highlight some words that the user selects.
Here is a snippet: I am trying to get the value of "lblinvnum".
<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="False" AllowPaging="True"
AllowSorting="True" DataSourceID="SqlDataSource1" Width="540px"
CssClass="Gridview" >
<HeaderStyle BackColor="#df5015" />
<Columns>
<asp:TemplateField HeaderText="Invnum" ItemStyle-Wrap="False">
<ItemTemplate>
<asp:Label ID="lblInvnum" Text='<%# HighlightText(Eval("Invnum").ToString()) %>' runat="server" />
</ItemTemplate>
...
...
... For the "AmtPaid" listed below, I can Get that value. I cannot figure out how to get the value for "lblInvnum" to the snippet listed above.
<asp:BoundField DataField="AmtPaid" HeaderText="AmtPaid"
SortExpression="AmtPaid" DataFormatString="{0:c}" >
In visual basic, this is how I can get the one above for AmtPaid.
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Dim selectedRow As GridViewRow = gvDetails.Rows(index)
Dim contactCell As TableCell = selectedRow.Cells(1)
Dim contact As String = contactCell.Text
Suggestions?

Related

Find Radiobutton Control from ID String in vb.net

I have 3 radiobuttons in my aspx:
<asp:RadioButton ID="rdoMasculin"
runat="server" AutoPostBack="true"
Checked="true" GroupName="gender"
TextAlign="Right" />
<asp:RadioButton ID="rdoFeminin"
runat="server" AutoPostBack="true"
GroupName="gender" />
<asp:RadioButton ID="rdoAnonymous"
runat="server" AutoPostBack="true"
GroupName="gender" />
When sending the form I'm storing the ID of the checked radiobutton in a Session variable:
for example: Session("currentGender") = rdoZukuTermine.ID
When I'm getting back on the page in a later stage I'm using the following code in codebehind:
Dim currentRadio = CType(FindControl(Session("currentGender")), RadioButton)
currentRadio.Checked = True
Session.Remove("currentGender")
currentRadio ends up as a null reference. But when I'm checking the Session it contains the right ID as a string.
Can someone help me?
Is the RadioButton control directly in your .aspx or does it have any parent controls? if yes call Findcontrol() on it direct parent like this:
Dim currentRadio = CType(ParentControl.FindControl(Session("currentGender")), RadioButton)

ASPxGridView GetSelectedFieldValues not retrieving values

I'm trying to make such a simple thing as getting one value from the selected row in an ASPxGridView with the GetSelectedFieldValues method with a button.
I've followed the steps from DevExpress documentation with the difference I want to retrieve the value inside a Label. Nothing difficult with classic ASPGridView.
Protected Sub ASPxButton1_Click(sender As Object, e As EventArgs) Handles ASPxButton1.Click
Dim oRowVal As List(Of Object)
Dim oVal As Object
oRowVal = ASPxGridView1.GetSelectedFieldValues({"No_"})
oVal = oRowVal(0)
Label1.Text = oVal.ToString
End Sub
As you can see, the thing I want is to get the selected value within the ASPxGridView and display it in a Label.
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<dx:ASPxButton ID="ASPxButton1" runat="server" AutoPostBack="False" Text="ASPxButton" Theme="BlackGlass" Width="150px">
</dx:ASPxButton>
<dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" EnableTheming="True" Theme="BlackGlass">
<Columns>
<dx:GridViewCommandColumn ShowSelectCheckbox="True" VisibleIndex="0">
<ClearFilterButton Visible="True">
</ClearFilterButton>
</dx:GridViewCommandColumn>
<dx:GridViewDataTextColumn Caption="GP" FieldName="No_" VisibleIndex="1">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn Caption="Description" FieldName="DescripciĆ³n Grupo" VisibleIndex="2">
</dx:GridViewDataTextColumn>
</Columns>
<SettingsBehavior AllowSelectSingleRowOnly="True" />
<Settings ShowFilterRow="True" ShowGroupPanel="True" />
</dx:ASPxGridView>
Thanks in advance!!!
Specify the ASPxGridView.KeyFieldName property in order to use the Selection state:
<dx:ASPxGridView ... KeyFieldName="...">
By the way, does the following code return the correct results?
oRowVal = ASPxGridView1.GetSelectedFieldValues({"No_"})
oVal = oRowVal(0)

Sort the Pictures in Datalist Control

I Have a Datalist Control and Reading the pictures from folder and showing them in lightbox from Datalist i am also reading data and time from those pictures.Ist thing is that the datalist is not reading the Date picture taken from the attributes and second is that pictures are not being sorted in desc order of date picture taken.Please anyone tell me how can i complete these taskes.
My data list code is here
<asp:DataList ID="DataList1" runat="server" RepeatColumns="5" RepeatDirection="Horizontal" Width="23%" CellPadding="2" CellSpacing="0" Height="270px" HorizontalAlign="Center">
<ItemTemplate>
<asp:Image Width="200px" height="200px" ID="Image1" ImageUrl='<%# Bind("Name", "~/MMS/1 Box/{0}") %>' runat="server" />
<br />
<asp:HyperLink ID="HyperLink1" rel="lightbox[roadtrip]" title='<%# DataBinder.Eval(Container.DataItem, "LastWriteTime", "{0:yyyy-MM-dd hh:mm:ss}") %>'
Text="Zoom" NavigateUrl='<%# Bind("Name", "~/MMS/1 Box/{0}") %>' runat="server"/>
</ItemTemplate>
<ItemStyle BorderColor="Silver" BorderStyle="Dotted" BorderWidth="1px" HorizontalAlign="Center"
VerticalAlign="Bottom" />
</asp:DataList>
and to read the Images from folder my vb.net code is here
Sub loadimage()
Dim sdir As New DirectoryInfo(MapPath("~/MMS/1 Box"))
Dim file As FileInfo() = sdir.GetFiles()
Dim list As New ArrayList()
For Each file2 As FileInfo In file
If file2.Extension = ".jpg" OrElse file2.Extension = ".gif" OrElse file2.Extension = ".bmp" Then
list.Add(file2)
End If
Next
DataList1.DataSource = list
DataList1.DataBind()
End Sub
Anyone tell me what change should i have to make in code so that my both requirements will be completed as well
Use IComparable to sort your list based on time creation time or whatever attribute you want to sort it.
Here is a link which would help you implement this: http://www.knowdotnet.com/articles/sortarraylistofobjects.html

how to retrieve binary image from database in vb.net and insert the image in grid view

How to retrieve a binary image from a database using vb.net and insert the image into a GridView.
This is my DB
image (id as integer , img as varbinary(max))
While you clarify the type of gridview you are referring to, here's how to insert the data in the database:
Using c As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
c.Open()
Dim command = New SqlCommand("INSERT INTO yourtable(image) values (#image)", c)
' this is specific to the FileUploadControl but the idea is to get the
'image in a byte array; however you do it, it doesn't matter
Dim buffer(FileUpload1.PostedFile.ContentLength) As Byte
FileUpload1.PostedFile.InputStream.Read(buffer, 0, buffer.Length)
command.Parameters.AddWithValue("#image", buffer)
command.ExecuteNonQuery()
End Using
And assuming you are talking about an ASP .NET app, you can bind the data to the gridview as follows:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<!--Trick to encode the bytes as a BASE64 string-->
<img width="100px" height="100px" src='data:image/png;base64,<%#System.Convert.ToBase64String(Eval("image"))%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Data Source=Your_ConnectionString_GoesHere"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT [id], [image] FROM [your_table_name_goes_here]">
</asp:SqlDataSource>

VB.net: Accessing data from DataList in Page_Load

New to VB.net, and trying to re-factor an 'old-skool' ASP page where all the page logic is happening on the .aspx page itself, to code-behind. Basically, I have a button that has a state, either on or off. If on, I set a hidden field to 1, if off, I set it to 0 (the default when a user visits the page).
The goal is to simply change the message I am sending to the user.
Have the following relevant code in MyPage.aspx:
<asp:HiddenField ID="hfldState" runat="server" Visible="false" Value="0" />
<div id="mainContent">
<asp:Literal ID="lblMessage" runat="server"
Visible="false" />
<asp:DataList ID="dlList" runat="server"
DataSourceID="sdsList"
DataKeyField="Entry No_"
RepeatLayout="Flow">
<ItemTemplate>
<div>
<asp:HyperLink ID="hlCurriculum" runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "Title") %>'
NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "File Path") %>'
ToolTip='<%# DataBinder.Eval(Container.DataItem, "Title") %>'
Target="_blank"
Style="font-weight: bold;">
</asp:HyperLink>
</div>
</ItemTemplate>
</asp:DataList>
</div>
<asp:SqlDataSource ID="sdsList" runat="server"
ConnectionString="..."
SelectCommand="SELECT [Entry No_], [Title], [File Path] FROM [Table] WHERE ([State] = #State)">
<SelectParameters>
<asp:ControlParameter ControlID="hfldState" Name="State" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
And the following in the Page_Load Sub:
If Page.IsPostBack Then
' Check for results
dlList.DataBind()
If dlList.Items.Count > 0 Then
' Results, display them
lblMessage.Text = "<h3>Results</h3>"
lblMessage.Visible = True
Else
' No results
lblMessage.Text = "<p>No Results</p>"
lblMessage.Visible = True
End If
Else
' user has not clicked anything
lblMessage.Text = "<p>Click button!</p>"
End If
The above code produces the following effect: when I click the button, setting the state to 1 and the page reloads, in Page_Load, I am getting zero results from the If dlList.Items.Count > 0 Then check, and thus am showing the 'No results' message, but the actual asp:DataList on the MyPage.aspx page is returning results... and then if I click the button again, setting it back to 0, in Page_Load, the datalist is now returning results, so I set the text to 'Results', but again, the control on the MyPage.aspx page behaves correctly, and shows no results, as expected. Now keep in mind that the above works perfectly in terms of checking the number of results if I move the If dlList.Items.Count > 0 Then page logic out of Page_Load and back to the MyPage.aspx file, it all works fine (i.e. results when state = 1, none when state = 0)
Any ideas?
I dont see a check in your page load for a postback:
If Not IsPostBack
'code
dList.DataBind() 'here is where you want to bind the data...
end if
The issue related to my ignorance of the page lifecycle in VB.net. Solution was to adjust the visibility of the asp:Literal, asp:DataList, set values etc in Page_PreRenderComplete. Basically, the asp:DataList wasn't being set in Page_Load, as expected, so doing any kind of logic in Page_Load based on the item count didn't make any sense.