Add static and dynamic values to ComboBox in vb.net - vb.net

I want to add 'All' option on top of other records fetched from database shown in dropdownlist.Any ideas on how to do this in vb.net 2005?

Think around the problem. Instead of adding the item to the combobox, add the item to the dynamic datasource.

Even in VS 2005 the AppendDataBoundItems property was available. So you can add this item programmatically from codebehind or declaratively on aspx:
<asp:DropDownList
runat="server"
ID="DropDownList1"
AppendDataBoundItems="true"
>
<asp:ListItem
Enabled="True"
Selected="True"
Text="All"
Value="0"
/>
</asp:DropDownList>
Now all your databound items are appended automatically and the first item will not be removed.

below worked for me!
Add a new row to datasource
Dim dr As DataRow = ds.Tables("cusPracticeLocation").NewRow
'Add some data to it
dr(0) = 0
dr(1) = "All"
ds.Tables("cusPracticeLocation").Rows.InsertAt(dr, 0)

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)

GridView in panel not loading while panel is hidden

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.

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.

ASP.net VB Listbox

I've seen many questions around this one, but none hitting it directly.
I put a listbox on a page and populate it with three items from an Access database. I have a button on that page that will extract several values including the selected item from the listbox. Or I want to anyway.
I can see the item selected in windows (highlighted) when I click the button, but when I try to select it no item is available as selected in the listbox. The ListBox1.SelectedIndex is alway -1.
Here is the code from the page:
<asp:ListBox ID="ListBox1" runat="server">
<asp:ListItem Text="List1" />
<asp:ListItem Text="List2" />
<asp:ListItem Text="List3" />
</asp:ListBox>
Is there a property missing?
Here is the code from the code behind page:
Public Function getDept() As String
Dim dept As String
If ListBox1.SelectedIndex > -1 Then
dept = ListBox1.SelectedItem.Text
Else
dept = "CMS"
End If
Return dept
End Function
Please help, I have until about noon to figure this out.
There may some reasons:
1- Check if your page's viewstate is true.
2- Call your method after Page_Load event.
Where do you call the function?
Consider that you should call it after Page_Load event. Also your viewstate

ComboBox's Selected Value Changed On Lost Focus in VB.NET

I have a datagridview(dgv) with a DataGridViewComboBoxColumn(colLocation)
colLocation.AutoComplete = False
colLocation.HeaderText = "Stored to"
colLocation.DataSource = DB.getLocation()
colLocation.DisplayMember = "description"
colLocation.ValueMember = "id"
I added the colLocation to dgv.
"descirption" contains Unicode characters. I can see the comboBox correctly and choose the item.
The problem is when the comboBox lost the focus, the value is changed to first item of the comboBox.
Any suggestion?
Updated:
I found out that the ComboBox doesn't change the data when the DisplayMember is in English characters.
It changes only when the DisplayMember is in Unicode chracter. Any idea for how could solve this? – tunwn 0 secs ago
Your .aspx page should have something like so:
<asp:DropDownList Width="90px" ID="ddlExpenseTypes" OnSelectedIndexChanged="ddlExpenseTypes_SelectedIndexChanged" DataSource='<%# GetExpenseTypes() %>' SelectedValue='<%# Bind("ExpenseReasonID") %>' DataTextField="ExpenseReasonID" DataValueField="ExpenseReasonID" AutoPostBack="true" runat="server" ></asp:DropDownList>
That is you need a functional call for index changed. A function call for filling the drop down ist (DataSource) and a function call to set the SelectedValue of the drop down list.
Is this DataGridView databound? You haven't specified a value for DataPropertyName, so there's nothing for colLocation.ValueMember's ID selection to bind to.