GridView in panel not loading while panel is hidden - sql

I have a page with 2 panels. Active panel is set by a dropdown. Panel A is set to visible="true", panel B is set to visible="false" by default.
Within each panel is a GridView. The visible panel grid will have data populated in it when the page loads, but the grid on panel B will display an empty set. If I hit the search button on that panel (for that specific grid), it will then populate data.
If I switch it around and make panel B visible & A hidden, grid B will have data and grid A won't. Search will then load it. If I set both panels to be visible, both grids will populate.
This is in my page load function:
If Not Page.IsPostBack Then
table_dropdown.SelectedIndex = 0 'setting the default to view Table A
TPAnnuity_SqlDataSource.SelectCommand = "SELECT * FROM TABLE A"
TPLife_SqlDataSource.SelectCommand = "SELECT * FROM TABLE B"
End If
And my various panel & grid declarations:
<asp:Panel ID="TPAnnuity_Panel" runat="server" visible="true">
<asp:GridView ID="TPAnnuity_GridView" AllowSorting="true" AllowPaging="true" Runat="server"
DataSourceID="TPAnnuity_SqlDataSource" DataKeyNames="AnnuityTotalPointsID"
AutoGenerateColumns="False" ShowFooter="true" PageSize="20">
</asp:GridView>
</asp:Panel>
<asp:Panel ID="TPLife_Panel" runat="server" visible="false">
<asp:GridView ID="TPLife_GridView" AllowSorting="true" AllowPaging="true" Runat="server"
DataSourceID="TPLife_SqlDataSource" DataKeyNames="LifeTotalPointsID"
AutoGenerateColumns="False" ShowFooter="true" PageSize="20">
</asp:GridView>
</asp:Panel>
What gives? Did I miss something blatant? I use setups like this on multiple other pages, and have compared. Can't see the issue.

The other examples that worked had the select command baked right into the .aspx file, like so:
<asp:SqlDataSource ID="TPAnnuity_SqlDataSource" Runat="server"
SelectCommand="SELECT * FROM Table A"
InsertCommand="INSERT INTO Table A () Values () "
UpdateCommand="UPDATE Table A Set Stuff = This">
I originally only had the Insert and Updates here, and had the data bind in the back end.

Related

Display image from database asp.net vb

I have database
Id int
img varbinary(MAX) --> image
descr nvarchar(50)
and now I want to display image and description. I use the following code
<div>
<asp:GridView
ID="GridView1"
runat="server"
AutoGenerateColumns="False"
DataKeyNames="Id"
DataSourceID="SqlDataSourceH"
CssClass="myGridView"
RowStyle-CssClass="rows"
ShowHeader="false">
<Columns>
<asp:ImageField
DataImageUrlField = "Id"
DataImageUrlFormatString = "Hardware.aspx?Id={0}"
ControlStyle-Width = "100"
ControlStyle-Height = "100"></asp:ImageField>
<asp:BoundField DataField="descr"></asp:BoundField>
</Columns>
</asp:GridView>
<asp:SqlDataSource
runat="server"
ID="SqlDataSourceH"
ConnectionString='<%$ ConnectionStrings:ConnectionString %>'
SelectCommand="SELECT [Id], [img], [descr] FROM [Hardware]">
</asp:SqlDataSource>
</div>
but image is not displayed
What should I do to display the picture?
Check /Images/theimage.jpg really exists then you need to go into the database and put the full path in the column img.
Also if it is in the folder /Images/ then you need "../Images/theimage.jpg" to go back to the root folder. Check the value of the url copy it to clipboard and dump it in the address bar and see if it comes up, so http://localhost/(dump url here) if it doesn't come up then you need to check the path and update the url value in database.

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)

How to get gridview clickable cell data value to new page aspx

I have two columns in a GridView where every cell of 1 column is hyperlinked.
<asp:GridView ID="gvBlogList" runat="server" DataKeyNames="Id"
AutoGenerateColumns="False" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLinkField DataTextField="id"
HeaderText="Blog id" NavigateUrl="detailspage.aspx" />
</ItemTemplate>
<ItemStyle Width="150px"></ItemStyle>
</asp:TemplateField>
</Columns>
</asp:GridView>
How can I get cell value in 2nd page as a session variable?
Thanks!
As per the what I understand from your question, you want to catch the value of the cell that is clicked, store it in session and make it accessible on the page that have redirect to. This is possible and you can try a flow like this :
You can use the GridView's inbuilt method onRowCommand in this case to generate a server side event and you can then identify which cell was clicked and lastly, you can get its value by index. The below code is a demonstration that you can try and manipulate as per your requirement. This is a sample where a Linkbutton is used in Gridview and on the click of it, its value is fetched.
You can also try to use the CommandName and CommandArgument properties if you want to assign that particular control a value that you may use at the server side.
protected void Grd1_RowCommand(object sender, GridViewCommandEventArgs e)
{
LinkButton lnk = (LinkButton)e.CommandSource;
string value = lnk.Text;
}
And for storing it in Session, you just have to make use of Session["yourVarName"] = value;. And this value can be accessed on the other page.
Hope this helps.

visual studio 2010 Getting value from gridview

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?

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.