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

9
Sep 09

How to change a body background in ASP.NET

This is something i had to look up. It’s realy easy though but it feels a bit hacky:

body id=”bodytag” runat=”server”

So as you see i have added a runat=”server” tag to the body tag. This basically makes sure our server code is aware of the body element.
I’ve also given it an id so i can address the control in the code-behind.

In the code-behind we can now add a style attribute (or class if you wish) to the bodytag:

bodytag.Attributes.Add(“style”, “background: url(‘images/logos/bmw.jpg’) #F0F0F0 right bottom no-repeat; height: 100%;”);

  • Share/Bookmark