C#

  • Find a texbox control or label control on a master page from an inner page

    I noticed that this one seems to catch a good few people out - including myself when I was starting out with Master Pages. If you have an aspx page that sits in your Master Page you might have a need of populating a label or a textbox (or other control) with some value. To do this you can simply use the following code to tell .net to find your control on your master page. In the example below I am looking for a Literal control but you can easily change this to TextBox or Label or whatever you want:

    Literal ControlIDToFind = (Literal)this.Master.FindControl("ControlIDToFind");

    Just change the ControlIDToFind to the name of your ID you want to grab and then you can use it just like you would a normal .net control on your child page. So using the example above, if I wanted to output text into my ControlIDToFind literal which is in my MasterPage I would use the following code in my Child Page:

    ControlIDToFind .Text = "some text about how great you are!";

     It's only a small 2 line thing but you'd be surprised how often this catches people out. Below is a small list of items you can find using the above tips, hope it helps!

    //find a dropdown in your masterpage and get it's selected value

    DropDownList CountryList= (DropDownList)this.Master.FindControl("CountryList");

    string SelectedCountryList = CountryList.SelectedValue;

    //find a textbox in your masterpage and get it's value

    TextBox EmailAddress = (TextBox)this.Master.FindControl("EmailAddress ");

    string EmailAddress = EmailAddress.Text;

    //find a label in your masterpage and get it's value

    Label EmailAddress = (Label)this.Master.FindControl("EmailAddress");

    string EmailAddress = EmailAddress.Text;

    C#
  • Hide a div element and it's contents using c#

    This can be a nice little trick to do if you want to hide options on a user after an update or after so many products are selected, etc. Simply wrap the area you want to hide in a div element and put a runat="server" value into it's tag. Example:

     

    <div id="GridDiv" runat="server">

    gridview would go in here....

    </div>

     

    You can now easily set the div to hidden using a bit of CSS that can be set using C# whenever your condition is met. For example:

    GridDiv.Attributes["style"] = "display:none;";

    //OR you could just type

    GridDiv.visible = false;

    That's all there is to it but you can see how powerful this can be as it allows you to dynamically set the css class or styles you might want in your applications.

    C#
  • Dynamically set the Body Onload attribute when using MasterPages

    If you use MasterPages in your .Net website there will be a time when you want to load some javascript on a certain page and have it called OnLoad within the Body tag. A good example is when you use Google Maps.

    When setting up your Google Map you will need to place javascript in the header of your page and you also need to set the Body OnLoad and UnLoad attributes. You could place the javascript in the MasterPage and be done with it but that is such a waste as usually you only want the Map to show on a contact page or similar. So how do you set the onBody attribute from within a child page?

    C#
  • how to read data from an Excelsheet using C#

    I had to use the script below to read in values from an Excel sheet using C# recently. The script itself is generic enough but it has one benefit - you don't need to know the name of the sheet you want to import. This can be very beneficial if you have people supplying you with Excel sheets as everyone has their own way of saving sheet names.

    The code in this example will take your Excel sheet and read in the first 5 columns. It will also loop through any other sheet and read in the first 5 columns in those too.

    C#
  • How to know which button is clicked on when using a repeater C#

    If you're developing websites, sooner or later you will be asked to produce a list of data where each row will need to have a button on it that performs a function. If you need to do this in a Repeater and are wondering how to achieve this then read on.

    For this example I will assume you know what a repeater is and are familiar with how to binding data to it from a database or similar.

    In your page you would have a repeater. Below is a sample that I'm going to use for this example:

    <asp:Repeater ID="MyRepeater" OnItemCommand="WhatToDo_ItemCommand" runat="server">
    <ItemTemplate>
    <ul>
        <li><%# Eval("ItemName")%></li>
        <li><%# Eval("ItemDetails")%></li>
        <li><asp:LinkButton ID="DeleteItem" CommandArgument='<%# Eval("ItemID") %>' CommandName="DeleteThisItem" runat="server">delete this item</asp:LinkButton></li>
        <li><asp:LinkButton ID="UpdateItem" CommandArgument='<%# Eval("ItemID") %>' CommandName="UpdateThisItem" runat="server">update this item</asp:LinkButton></li>
    </ul>
    </ItemTemplate>
    </asp:Repeater>

    Nothing to out of the ordinary here. There are a few things to note. In your <asp:repeater> tag you must include the class you want to call when the button is clicked. This is the OnItemCommand="WhatToDo_ItemCommand" bit. You will see in a few mins where this is used. Next up is the code that puts the ID value into the buttons you're going to use. CommandArgument holds the value for your command, in this case it's going to hold our database row ID and CommandName is needed so that our code will know which button made the call.

    Next up is the code behind page:

    C#

Get In Touch

Follow me online at TwitterFacebook or Flickr.

Latest Tweets