ASP.net VB Listbox - vb.net

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

Related

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.

Get DropDownList Selected Value by Selected Text

Do anyone knows how to get selected value of a drop down list by selected text?
I tried the below method but doesn't work:
ddlWorkType.SelectedItem.Text = "writing"
myddlvalue = ddlWorkType.SelectedValue
Please show me how. Thanks.
DDLResp.Items.FindByText(TxtResp.Text.Trim).Selected = True
Try this Code
how to get selected value and selected text in vb.net
<asp:DropDownList CssClass="textbox" ID="ddlUser" runat="server">
<asp:ListItem Value="0">Select User</asp:ListItem>
</asp:DropDownList><br />
Selected DropDownList value
ddlvalue = ddlUser.SelectedItem.Value
Selected DropDownList text
ddlText = ddlUser.SelectedItem.Text
Selected text is actually text which you selecting through mouse drag or by press shift key to copy/paste or delete text. You need to use just .Text instead of .SelectedText
ddlWorkType.Text = "writing"
myddlvalue = ddlWorkType.SelectedValue
if the text does not exist in assign datasource then combobox SelectedIndex will return -1 and SelectedValue will return Nothing.

asp.net dropdown listbox not holding viewstate

I am using VS 2008, VB and using a dorpdown listbox in my asp.net webpage. I select a value from the dropdown, click the submit button, when the page comes back from the server, the value in the dropdown is blank (default). The dropdown controls viewstate is enabled.
The code for the submit button is:
<asp:Button ID="ButtonSubmit" runat="server" Text="Submit" CssClass="Button" />
The dropdownlist code is:
What am I missing here?
are you binding dropdown in page_load event? If yes move that code in
if(!page.isPostback)
{
binddropdown();
}

LinqDataSource "OnSelecting" event not firing on Post-backs

I have a web page with two dropdownlist controls, each bound to separate LinqDataSource objects. One displays a list of Categories and the other displays a list of Articles. The Category choice drives Article list (at least, that's my intended behaviour). The Article list is also filtered based on the users language preference, stored in the Session and applied in the ArticleLinqDataSource_Selecting event handler.
<asp:Label runat="server" Text="Category Code:" AssociatedControlID="CategoryDropDownList" />
<asp:DropDownList runat="server" ID="CategoryDropDownList" DataSourceID="CategoryLinqDataSource" DataValueField="CategoryID" DataTextField="CategoryCode" AutoPostBack="true" />
...
<asp:Label runat="server" Text="Article Code:" AssociatedControlID="ArticleCodeDropDown" />
<asp:DropDownList runat="server" ID="ArticleCodeDropDown" DataSourceID="ArticleLinqDataSource" DataValueField="ArticleID" DataTextField="ArticleCode" OnDataBound="ArticleCodeDropDown_DataBound" />
...
<asp:LinqDataSource runat="server" ID="CategoryLinqDataSource"
ContextTypeName="Article.Data.ArticleDataContext"
TableName="Categories" Select="new (CategoryID, CategoryCode)">
</asp:LinqDataSource>
...
<asp:LinqDataSource runat="server" ID="ArticleLinqDataSource"
ContextTypeName="Arcicle.Data.ArticleDataContext"
TableName="Articles" OrderBy="ArticleCode"
Select="new (ArticleID, ArticleCode)"
OnSelecting="ArticleLinqDataSource_Selecting">
</asp:LinqDataSource>
This all works fine when the page first loads. The Category list contains the all the available category values, and the first category in the list is selected. And the corresponding Articles for the first category are displayed appropriately in the Article dropdown control. However, when I change the category (a post-back happens because I have it set to AutoPostBack="true", the Article dropdown does not get refreshed. In other words, the OnSelecting event is not getting fired on subsequent postbacks. Is this the expected bahaviour? If it is, how do I get around this?
You need to call DataBind on the DropDownList in the CategoryDropDownList's SelectedIndexChanged event. It doesn't happen on its own.
e.g.
Protected Sub CategoryDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CategoryDropDownList.SelectedIndexChanged
ArticleCodeDropDown.DataBind()
End Sub

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.