Category Archives: ObjectDataSource

[RESOLVED] ddl selected default item

Hi, I am binding data to ddl


            ddl.DataSource = source;
            ddl.DataTextField = "des";
            ddl.DataValueField = "code";
            ddl.SelectedValue = "999";
            ddl.DataBind();

How do I make the value 999 to show first item in the ddl? if I use selectedValue (at the source it shows its selected as default but not in the UI)

Thanks,

Try this?

ddl.DataSource = source;
            ddl.DataTextField = "des";
            ddl.DataValueField = "code";
            ddl.DataBind();
            ddl.SelectedValue = "999";

If that doesn't work (I dont think it will) then try this:

Create a dll_DataBound event handler and put this code in it:


DropDownList ddl = (DropDownList)sender;

ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText("999"));
// or
ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue("999"));

// depending on whether its the text or the value you want to match

rtpHarry

Try this?

 

ddl.DataSource = source;
            ddl.DataTextField = "des";
            ddl.DataValueField = "code";
            ddl.DataBind();
            ddl.SelectedValue = "999";

Ah that worked then? I'm not too familiar with manual databinding as I always prefer to wrap my code up in a [DataObject()] and feed it through an objectdatasource.

Glad it worked anyway!

[RESOLVED] Just got vs2010, no show data sources in menu for web forms

Hi.

In windows forms, there is the "show data sources" under the "data" menu item, and I love it because I can drag and drop the fields I need onto the form and it makes the control automatically.

In web forms however, this option isn't there.  Why?  I try to drag and drop fields from the "server explorer" but it generates grids, I don't want grids I just want to drag and drop the text boxes or checkboxes etc.

Is there a way to do this with vs 2010?  Thanks.

-JBD

If you want an easy way to pull data in and bind it to display a single record then you should use the FormView control (if you want control over the markup) or DetailsView if you want the table to be generated for you:

Wow thanks, I never used formview before, it should save a ton of time.

No problem.

I like to wire it up using an ObjectDataSource and then plug in my own class to the ObjectDataSource by annotating it with [DataObject(true)] and other attributes. That way the IDE can automatically pick it up and you dont have to manually select each of the select, insert, delete, update statements.

(scroll down about 85% of the way to see a code sample)

Do you happen to know of a way to make the formview and / or dataview  load with the EditItemTemplate?  Or would I have to manually edit every field in itemTemplate?

 

EDIT: Nevermind :)  I set the default mode to edit in the properties derrr :)

[RESOLVED] DropDownList DataValueField not setting

It's been a while since I've been using ASP.NET, so I'm a bit rusty - but I've got a really infuriating problem that seems like it should be really straightforward and I can't get my head around it.

I have an object data source linked to a generic list of objects that have a Name and Source property.  This is working fine:

<asp:ObjectDataSource runat="server"
                          ID="odsNewsSources"
                          SelectMethod="DataSources"
                          TypeName="HCR.Portal.NewsDataSource" />

Then I have a dropdown list that uses this data source:

<asp:DropDownList runat="server" 
                                          ID="ddlDataSource"
                                          DataSourceID="odsNewsSources" 
                                          DataTextField="Name"
                                          DataValueFied="Source" 
                                          AutoPostBack="true"
                                          OnSelectedIndexChanged="ddlDataSource_SelectedIndexChanged" />

The problem I have is that the selected value of the ddl is always returns the 'Name' value, not the 'Source' value as specified as teh DataValueField. If I set the DataTextField to 'Source', I get the correct operation of the dropdown, so everything else seems to be fine - it's purely that the value field is not picking up 'Source' as it should.

Any ideas?

u need to do this to get the value of seleted text

ddlDataSource.Selectedvalue

show the code of you "ddlDataSource_SelectedIndexChanged" event

you have 2 properties in the selectedItem that you can use:

  • ddlDataSource.SelectedItem.Text : used to get the text of the selected item
  • ddlDataSource.SelectedItem.Value : used to get the value of the selected item (which what you need)

or you access the selected value by using : ddlDataSource.SelectedValue

I've found a workaround by setting the DataSource in the code behind and not using the ObjectDataSource:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddlDataSource.DataSource = HCR.Portal.NewsDataSource.DataSources();
            ddlDataSource.DataTextField = "Name";
            ddlDataSource.DataValueField = "Source";
            ddlDataSource.DataBind();
        }
        
        //Master.InitializeCurrentSession();
    }

It now works as expected. I have no idea what the difference is between this and the ObjectDataSource I was using, but it does now work.

The SelectedIndexChanged method is as follows:

protected void ddlDataSource_SelectedIndexChanged(object sender, EventArgs e)
    {
        RssDataSource1.Url = (sender as DropDownList).SelectedValue;
        lstRecentNews.DataBind();
    }

Using the ObjectDataSource the dropdown SelectedValue returns the value specified for the DataTextField.  Using the Code behind to set the dropdown data source it returns the value specified for the DataValueField.

[RESOLVED] Creating unique details pages from databound hyperlinks

I'm currently making a site that is supposed to generate search results from a separate assembly, with a gridview that has links to each robot. The links from the gridview are supposed to link to a unique robot details page, as well as display another gridview to all related robots from the set, like this:

 

[IMG]http://www.sawauto.com/uploader/up/2011-03-14_132416.jpg[/IMG]

 

I'm stuck on how to display unique detail pages. So far, I only have a details page template, but I'm trying to figure out how I can display the data dynamically, depending on which robot was selected from the main search.

 

Here are my Default.aspx and Details.aspx mark-ups so far:

 

Default Pastebin

 

Details Pastebin

 

And the resulting Default and Details pages:

[IMG]http://www.sawauto.com/uploader/up/2011-03-14_133933.jpg[/IMG]

 

[IMG]http://www.sawauto.com/uploader/up/2011-03-14_133107.jpg[/IMG]

Hi,

You need to pass the selected robot id from search page to your details page.

I've found you have a parameter called "id" in your detail page. 

  1. <div style="font: normal normal normal 1em/1.2em monospace; background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; vertical-align: top; line-height: 19px; background-position: initial initial; background-repeat: initial initial; padding: 0px; margin: 0px;" class="de2">        <asp:ObjectDataSource ID="RobotaPartnersSdkDetails" runat="server"</div>
  2. <div style="font: normal normal normal 1em/1.2em monospace; background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; vertical-align: top; line-height: 19px; background-position: initial initial; background-repeat: initial initial; padding: 0px; margin: 0px;" class="de1">            SelectMethod="GetByID" TypeName="RobotaPartnersSdk.RobotCollection"></div>
  3. <div style="font: normal normal normal 1em/1.2em monospace; background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; vertical-align: top; line-height: 19px; background-position: initial initial; background-repeat: initial initial; padding: 0px; margin: 0px;" class="de2">            <SelectParameters></div>
  4. <div style="font: normal normal normal 1em/1.2em monospace; background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; vertical-align: top; line-height: 19px; background-position: initial initial; background-repeat: initial initial; padding: 0px; margin: 0px;" class="de1">                <asp:Parameter Name="id" Type="Int32" /></div>
  5. <div style="font: normal normal normal 1em/1.2em monospace; background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; vertical-align: top; line-height: 19px; background-position: initial initial; background-repeat: initial initial; padding: 0px; margin: 0px;" class="de2">            </SelectParameters></div>
  6. <div style="font: normal normal normal 1em/1.2em monospace; background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; vertical-align: top; line-height: 19px; background-position: initial initial; background-repeat: initial initial; padding: 0px; margin: 0px;" class="de1">        </asp:ObjectDataSource></div>

But I cannot find any binding code about it. So that's why you got a empty details page.

you can pass this robot id either through querystring or session.

Regards,

[RESOLVED] Binding a DropdownList to a Dictionary object "directly" (on the page)

Hi, I've got a Dictionary<string, string> object I'm creating:-

Dictionary<string, string> locationList = new Dictionary<string, string> { { "A", "AAA" }, { "B", "BBB" }, { "C", "CCC" } };

and I wanted to bind this to a dropdownlist.  I know this is possible in code-behind, but is it possible to do this directly on the asp: control, something like:-

<asp:DropDownList ID="DropDownList1" runat="server" DataSource="locationList" DataTextField="Key" DataValueField="Value">

I've seen various options, for example DataSource='<%#locationList.Values %>' , but can't get any of them to work and most solutions seem to fall back to codebehind.  For various reasons I'd rather not have to do it in codebehind, one being the simplicity/neatness of being able to do this in the asp: tag rather than having to have 2 "sections" of code.  Is this possible, or do I have to do this via codebehind like this?

            DropDownList1.DataSource = locationList;
            DropDownList1.DataTextField = "Value";
            DropDownList1.DataValueField = "Key";
            DropDownList1.DataBind();

At least you need a DataBind Command to bind the DropDownlist.

<asp:DropDownList ID="DropDownList1" runat="server" DataSource="<%# LocationList %>"
    DataTextField="Key" DataValueField="Value">
</asp:DropDownList>

 Create a property to bind the DropDownlist

public Dictionary<stringstring> LocationList
{
    get
    {
        return new Dictionary<stringstring> { { "A""AAA" }, { "B""BBB" }, { "C""CCC" } };
    }
}

 And datadind the list in page load

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
        DropDownList1.DataBind();
}

 That should show three values in DropDownList.  That's it.

Thanks for that, but I'm trying to avoid putting _any_ code in the code-behind (apart from, ofc, the dictionary object definition).  If that DataSource="<%# LocationList %>" property declaration were to a dataset object it wouldn't require a separate databind method invocation, why would it require one for the dictionary object?  On another solution tack, it doesn't _have_ to be a dictionary object - all I need is to be able to declare a key-value pair style object in the code-behind that I can interrogate using either the key or the value and then bind those pairs somehow to the dropdownlist object in the page code - if I have to create a single linking object that the DDL can "auto-bind" (in the same way it would to a dataset object) to then that would be fine - is there a neat way of doing it that way?

Hi,

Mad-Halfling

If that DataSource="<%# LocationList %>" property declaration were to a dataset object it wouldn't require a separate databind method invocation, why would it require one for the dictionary object? 

According to your description ,I would like to suggest you to check the link for the article which describes about the difference between <%= and <%# in ASP.NET.

http://blogs.msdn.com/b/dancre/archive/2007/02/13/the-difference-between-lt-and-lt-in-asp-net.aspx

Content of the link above:

What you can see as a result is:

•The <%= expressions are evaluated at render time

•The <%# expressions are evaluated at DataBind() time and are not evaluated at all if DataBind() is not called.

•<%# expressions can be used as properties in server-side controls. <%= expressions cannot.

You can check the link below for more information:

http://naspinski.net/post/inline-aspnet-tags-sorting-them-all-out-(3c25242c-3c253d2c-3c252c-3c252c-etc).aspx

 

Mad-Halfling

For various reasons I'd rather not have to do it in codebehind, one being the simplicity/neatness of being able to do this in the asp: tag rather than having to have 2 "sections" of code.
 

 

Mad-Halfling

if I have to create a single linking object that the DDL can "auto-bind" (in the same way it would to a dataset object) to then that would be fine - is there a neat way of doing it that way?
 

As far as I know ,after the particular data sources have been determined and set for the objects on the .aspx page, you must bind the data to these data sources. You can use the Page.DataBind or the Control.DataBind method to bind the data to the data sources.Both methods work similarly. The main difference is that all data sources are bound to their server controls after the Page.DataBind method is called. No data is rendered to the control until you explicitly call either the DataBind method of the Web server control or until you invoke the page-level Page.DataBind method. Typically, Page.DataBind (or DataBind) is called from the Page_Load event.

Please check the link below for more information:

ASP.NET data binding

Hope it can help you.

Erm, admins, please give the OP a chance to go through answers before marking them as best answer, this post is only a day old and I haven't had a chance to see if it answers the question.  I would also suggest that you check other posts to see if they need credit (for example: Sansan) rather than just selecting the admin's post!

I don't think you can bind a DropDownList without calling DataBind method as you are setting the DataSource property not DataSourceID. Another way is using your custom DataSource such as ObjectDataSource and set the ObjectDataSourceID for the DropDownList

<asp:ObjectDataSource runat="server" ID="odsLocationList" SelectMethod="GetLocationList"
    TypeName="Example.TestPage"></asp:ObjectDataSource>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="odsLocationList"
    DataTextField="Key" DataValueField="Value">
</asp:DropDownList>
namespace Example
{
    public partial class TestPage : System.Web.UI.Page
    {
        public Dictionary<stringstring> GetLocationList()
        {
            return new Dictionary<stringstring> { { "A""AAA" }, { "B""BBB" }, { "C""CCC" } };
        }
    }
}

That's the simplest way I can think of. Update your local collection in your codebehind page whenever needed and return that collection from the Select Method of the ObjectDataSource. That's it

Aha, that's exactly what I wanted - I did play around with the ODS but I hadn't had time to get it going fully and I was trying ot bind to an object rather than a method.

Any idea how do I would find out the correct page name for the page to put in the TypeName?  I'm trying to add this into someone else's page but there doesn't seem to be any namespacing set up in the code and I can't find anything anywhere where it's declared?

It seems like you are using Website project. Add that method to a class and put it in App_Code directory.

Thanks for that, I was being a brief giblet-head and trying to do too many things at once =8)