Archive for the ‘C#’ Category

Storing an arbitrary value into a cookie on the client’s machine is something that most site will require at one point or another. It allows for retrieving data across page loads. Below is code snippet of the simple input form in our example.

<form id=”form1″ runat=”server”>
<div>
Cookie Name:
<asp:TextBox ID=”txtCookieName” runat=”server”></asp:TextBox>
<asp:Button ID=”btnSetCookie” runat=”server” Text=”Set Cookie” onclick=”btnSetCookie_Click” />
</div>
</form>

Our next step is to define Button (btnSetCookie) event. Here, we create a HttpCookie. Next, create the cookie’s name and assign the value (as defined in the TextBox txtCookieName). Additionally, we will set the cookie to expire in one minute.

protected void btnSetCookie_Click(object sender, EventArgs e)
{
HttpCookie myCookie = new HttpCookie(”TestCookie”, txtCookieName.Text);
DateTime dtNow = DateTime.Now;
TimeSpan tsMinute = new TimeSpan(0, 0, 1, 0);
myCookie.Expires = dtNow + tsMinute;
}

In this situation, whenever a user clicks the Set button, a cookie (defined as myCookie) will be set that will expire in one minute.

Trying to access a specific GridView’s column is especially easy, but only if you know the column index. One way to handle accessing the column is to hard code the column index into the code:

GridView gv = new GridView();
DataControlField dcf = gv.Columns[5];

The main problem with the above solution is if you add a new column at index 1, you will need to manually change all hardcoded indexes. Therefore, another way to handle this is to set a constant integer for each column containing the index.

const int _MYCOLUMN = 5;
GridView gv = new GridView();
DataControlField dcf = gv.Columns[5];

Using this would only require you to change the _MYCOLUMN constant integer in one location, which would reflect in the rest of your code. However, an even better way to handle this is to access the column by a defined name. The method below involves setting the AccessibleHeaderText of a column on the aspx page when the gridview column is defined.

<asp:GridView ID=”GridView1″ runat=”server” AutoGenerateColumns=”False”>
<Columns>
<asp:BoundField AccessibleHeaderText=”date” DataField=”date” HeaderText=”Date” SortExpression=”date” />
<asp:BoundField AccessibleHeaderText=”article” DataField=”article” HeaderText=”Article” SortExpression=”article” />
</Columns>
</asp:GridView>

By setting the AccessibleHeaderText, we can now access the column through this header name. The function below requires the gridview and a string containing the AccessibleHeaderText. The function will return the index of the column, if found . Otherwise, negative one (-1).

private int findColumnIndex(GridView gridView, string accessibleHeaderText)
{
for (int index = 0; index < gridView.Columns.Count; index++)
{
if (String.Compare(gridView.Columns[index].AccessibleHeaderText, accessibleHeaderText, true) == 0)
return index;
}
return -1;
}

By using this method, adding a new column in index one will not affect the rest of the code. The method checks the AccessibleHeaderText of each column to verify that it matches your specified string. If it matches, it returns the index.

int dateIndex = findColumnIndex(GridView1, “date”);
int authorIndex = findColumnIndex(GridView1, “author”);
int articleIndex = findColumnIndex(GridView1, “article”);

In the above example, dateIndex will return 0, authorIndex will return -1, and articleIndex will return 1. It’s as simple as that!

It is natural for web users to hit the enter key to submit a form or otherwise activate an action. There have been several cases where the form does not submit on the enter key. The below example works.

<body>
<form id=”form1″ runat=”server”>
<div>
<asp:TextBox ID=”txtFirstName” runat=”server”></asp:TextBox>
<asp:Button ID=”Button1″ runat=”server” Text=”Button” onclick=”Button1_Click” />
<asp:Label ID=”lblOutput” runat=”server” Text=”"></asp:Label>
</div>
</form>
</body>

The Button1_Click action simply copies the input text from txtFirstName into the label lblOutput. Because this simple example works, one would expect this to work in every case. That is not the case. If hitting the enter key does not submit the form, try one of the following below:

  1. Try putting an invisible HTML Input (Submit) button. Though this has worked for others, it did not work for my situation.

    <input type=”text” style=”display:none”/>

  2. A second option to try is to use a onKeyDown event. This second option will append a moderately long javascript script to check for the enter key on each button press. If the button is pressed, it activates the form submit. To use this, place this script in the head. Finally, on the last input textbox, use the following:

    onKeyPress=”return submitFormWithEnter(this,event)”

    The script is below goes in the within html head tags:

    <script type=”text/javascript”>

    function submitFormWithEnter(myfield,e)
    {
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;

    if (keycode == 13)
    {
    myfield.form.submit();
    return false;
    }
    else
    return true;
    }
    </script>

  3. This last situation, which worked best for me also checks for the enter key using javascript, but instead of simply activating the form’s action, the function to be run is directly specified. Below, EmailTextBox is the last input TextBox element. Next, replace LoginButton with the ASP Submit Buttton. This script needs to go in the Page_Load section of the codebehind.

    EmailTextBox.Attributes.Add(”onKeyPress”, “javascript:if (event.keyCode == 13) __doPostBack(’” + LoginButton.UniqueID + “‘,”)”);