ASP.NET


12
Sep 09

ASP.NET Radiobuttons in a Gridview

Here’s another one of those classic Gridview problems:

I’ve got a Gridview that needs to have a RadioButtonList in it so that users can select only one row.

You can’t do this in ASP.NET with use of the <asp:RadioButton runat=”server”  />. Even though you can fill in a groupname it doesn’t work.
The reason it doesn’t work is that the rendered radiobuttons all have a different “name” attribute (you know, because the namingcontainer requires that all controls have a unique id).

The solution is very easy though; instead of using the .NET radiobutton control we will use a plain old <input type=”radio” />

Just like this:

<input type=”radio” name=”defaultfilegroup” value=’<%# Eval(“resource”) %>’ />

The name attribute works like the groupname; make it equal for all relevant radiobuttons.
The value attribute will contain the value of what the user selected. This is what we’ll retrieve as the selected option.

Retrieving the selected option:

supposing you save all changes when the user hits the “save” button here’s what we’ll do:

    public void SaveSetupLinkButton_Click(object sender, EventArgs e)
    {
        string selectedFile = Request["defaultfilegroup"];
    }

Whoa! That’s how easy it is.

Like this? Use the share buttons below!

  • Share/Bookmark

12
Jul 09

Consuming a simple WCF service

In my previous post, a simple wcf service, i showed how to make a WCF service that can return an array of strings.

In this post i will show you how to make a client in a regular ASP.NET website that can question this service and display the returned value on a page.
Start off by creating a new Website:

new-aspnet-project
I selected “ASP.NET Web Site”, picked .NET Framework 3.5 and i gave the thing a custom name.
Visual Studio will now create the files we typically need in a .NET website. Let’s just leave that now for what it is and make the reference to our service allready.

Adding a reference to the WCF Service

So in order to use our service in our website we will have to make some sort of reference to it. Luckily, Visual Studio can do nearly all the work for us.
Make sure your service is up and running (either on your localhost or somewhere else). You’ll need to know the address on which it is running so if you’ve got the service running on your local computer you might as well look in the WCF Test Client to see the exact url of the service.

In our web project right click the project and choose “”Add Service Reference”:
select-add-service-reference

Now with the Service reference dialog open we can input the address of our service and add it to our project. Notice that by doing this our IService contract is being used to communicate the functions of our service:

add-service-url-reference

Just click “Ok” and Visual Studio will take care of the rest. I will explain in a future post what exactly the files are that are being constructed since this is supposed to be a brief demo.

Databinding from the Service to a gridview

We’re nearly done.
Drag a gridview into the markup of your default.aspx and put AutoGenerateColumns on “True”. This is merely because i’m too lazy to create the colums myself.
Give your gridview and appropriate name like BrandsGridView.

In the codebehind we can now implement this code to get the info from the service and databind it:

protected void Page_Load(object sender, EventArgs e)
{
      string[] brandsList;
      ServiceReference1.CarServiceClient wcfService = new ServiceReference1.CarServiceClient();
      try{
            //i added an integer parameter to the get method so i can later use it to select the brands from a certain collection
           brandsList = wcfService.GetBrands(0);
           wcfService.Close(); //don't forget this!
        }
        catch (Exception)
        {
            wcfService.Abort();
        }

        BrandGridView.DataSource = brandsList;
        BrandGridView.DataBind();
    }

And when we run this website (F5) all should work out just fine:
final-result

Next stop on my WCF journey will be DataContracts; juggeling with object from service to client and vice versa.

  • Share/Bookmark