Monday 10 December 2012

Bind RowIndex to CommandArgument

This code used to pass rowindex for the button when using rowcommand

ASPX
<asp:Button id="Button1" runat="server" CommandName="MyCommand" Text="KlikMe" 
CommandArgument='<%# Container.DataItemIndex %>' /> 

Friday 7 December 2012

NO CODING : Filter Gridview with Radiobuttonlist

This No code behind binding
Table Name : JOBTABLE
Fields : JobId, Position, Company, Location, Experience, Information

ASPX
Location
    <asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True" 
        DataSourceID="SqlDataSource1" DataTextField="Location" 
        DataValueField="Location" RepeatDirection="Horizontal" RepeatLayout="Flow">
    </asp:CheckBoxList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:TESTConnectionString %>" 
        SelectCommand="SELECT DISTINCT [Location] FROM [JOBTABLE] WHERE ([Location] IS NOT NULL) ORDER BY [Location]">
    </asp:SqlDataSource>
    <asp:HiddenField ID="HiddenField1" runat="server" />
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="JobId" DataSourceID="SqlDataSource2">
        <Columns>
            <asp:BoundField DataField="JobId" HeaderText="JobId" InsertVisible="False" 
                ReadOnly="True" SortExpression="JobId" />
            <asp:BoundField DataField="Position" HeaderText="Position" 
                SortExpression="Position" />
            <asp:BoundField DataField="Company" HeaderText="Company" 
                SortExpression="Company" />
            <asp:BoundField DataField="Location" HeaderText="Location" 
                SortExpression="Location" />
            <asp:BoundField DataField="Experience" HeaderText="Experience" 
                SortExpression="Experience" />
            <asp:BoundField DataField="Information" HeaderText="Information" 
                SortExpression="Information" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
        ConnectionString="<%$ ConnectionStrings:TESTConnectionString %>" 
        SelectCommand="SELECT * FROM [JOBTABLE] WHERE (@Location LIKE '%' + [Location] + '%')">
        <SelectParameters>
            <asp:ControlParameter ControlID="HiddenField1" Name="Location" 
                PropertyName="Value" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>

C#
protected void Page_Load(object sender, EventArgs e)
    {
        HiddenField1.Value = "";
        foreach (ListItem li in CheckBoxList1.Items)
        {
            if (li.Selected == true)
            {
                HiddenField1.Value = HiddenField1.Value + "," + li.Text;
            }

        }
    }

Monday 3 December 2012

Replace/Remove unwanted character using regex

Sometime when we have fileupload that you want to save it to web server there are some anwanted character need to be changed becouse this character can make the download link not work
To change the chararacter use code below

C#
Regex reg = new Regex("[;\\/:*?\"<>|&']");
documentName = reg.Replace(documentName, "-");

Sunday 2 December 2012

CODING : Display Long Date Formatted Regarding LocalCulture RSS

C#
using System.Globalization;
using System.Threading;
private void Page_Load(object sender, System.EventArgs e)
{
    Thread.CurrentThread.CurrentCulture = 
        CultureInfo.CreateSpecificCulture(Request.UserLanguages[0].ToString());
    Thread.CurrentThread.CurrentUICulture = 
        new CultureInfo(Request.UserLanguages[0].ToString());
    lblToday.Text = Today;
    lblMessage.Text = "Culture Info Display Name " + 
                      Thread.CurrentThread.CurrentCulture.DisplayName;
}

NO CODING : 3 Nested Gridviews

Table Name : Music
Table Fields : Genre, Album., Title

Dont need writing VB/C# code for this sample



I like design mode, dont like writing code too much. So, this is my sample code creating NESTED Gridviews without VB/C# code. To use this code simply create table like above and create connection string in web.config called "TESTConnectionString" and copy-paste code below to your aspx page.

ASPX
<form id="form1" runat="server">
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" 
        CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="Black" 
        GridLines="Horizontal" ShowHeader="False" Width="100%">
        <Columns>
            <asp:TemplateField HeaderText="Genre" SortExpression="Genre">
                <ItemTemplate>
                    Genre :
                    <asp:Label ID="Label1" runat="server" style="color: #FF0000; font-weight: 700;" 
                        Text='<%# Bind("Genre") %>'></asp:Label>
                    <br />
                    <table class="style1">
                        <tr>
                            <td class="style2">
                                &nbsp;</td>
                            <td>
                                <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
                                    DataSourceID="SqlDataSource1" GridLines="None" ShowHeader="False" Width="100%">
                                    <Columns>
                                        <asp:TemplateField HeaderText="Album" SortExpression="Album">
                                            <ItemTemplate>
                                                Album :
                                                <asp:Label ID="Label1" runat="server" style="color: #0000FF; font-weight: 700" 
                                                    Text='<%# Bind("Album") %>'></asp:Label>
                                                <asp:HiddenField ID="HiddenField1" runat="server" 
                                                    Value='<%# Bind("Genre") %>' />
                                                <table class="style1">
                                                    <tr>
                                                        <td class="style2">
                                                            &nbsp;</td>
                                                        <td>
                                                            <asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" 
                                                                BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" 
                                                                CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="Black" 
                                                                GridLines="Vertical" ShowHeader="False" Width="100%">
                                                                <RowStyle BackColor="#F7F7DE" />
                                                                <Columns>
                                                                    <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                                                                </Columns>
                                                                <FooterStyle BackColor="#CCCC99" />
                                                                <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
                                                                <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
                                                                <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
                                                                <AlternatingRowStyle BackColor="White" />
                                                            </asp:GridView>
                                                        </td>
                                                    </tr>
                                                </table>
                                                <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                                                    ConnectionString="<%$ ConnectionStrings:TESTConnectionString %>" 
                                                    SelectCommand="SELECT [Title] FROM [Music] WHERE (([Album] = @Album) AND ([Genre] = @Genre)) ORDER BY [Title]">
                                                    <SelectParameters>
                                                        <asp:ControlParameter ControlID="Label1" Name="Album" PropertyName="Text" 
                                                            Type="String" />
                                                        <asp:ControlParameter ControlID="HiddenField1" Name="Genre" 
                                                            PropertyName="Value" Type="String" />
                                                    </SelectParameters>
                                                </asp:SqlDataSource>
                                            </ItemTemplate>
                                            <EditItemTemplate>
                                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Album") %>'></asp:TextBox>
                                            </EditItemTemplate>
                                        </asp:TemplateField>
                                    </Columns>
                                </asp:GridView>
                            </td>
                        </tr>
                    </table>
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:TESTConnectionString %>" 
                        SelectCommand="SELECT DISTINCT [Album], [Genre] FROM [Music] WHERE (([Genre] = @Genre) AND ([Album] IS NOT NULL))">
                        <SelectParameters>
                            <asp:ControlParameter ControlID="Label1" Name="Genre" PropertyName="Text" 
                                Type="String" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Genre") %>'></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
        <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
        <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:TESTConnectionString %>" 
        SelectCommand="SELECT DISTINCT [Genre] FROM [Music] WHERE ([Genre] IS NOT NULL)">
    </asp:SqlDataSource>
    </form>

Saturday 1 December 2012

NO CODING : Filter Gridview with Cascade Dropdownlist and Textbox

Technique filter gridview without any CODE.
I like simple way, dont like to write code too much. I am not typist, so generally using the design mode to create aspx page. I'm not so concerned with control ID, i think its not important in design mode.
Simply pick .. pick ..pick ...



Sorry if my sample have dropdownlist databound event. Only want insert "Show All" item in dropdown. I cannotd do it directly to the ddl html tag by enableappend, because result problem.

The sample using Music table with GENRE, ALBUM and TITLE fields

Table Name : Music
Table Fields : Genre, Album., Title

To use this code simply create table like above and create connection string in web.config called "TESTConnectionString" and copy-paste code below to your aspx page.

This is the sample
ASPX

<form id="form1" runat="server">
  <div>
        <table>
            <tr>
                <td class="style1">
                    Genre</td>
                <td>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
                        DataSourceID="SqlDataSource1" DataTextField="Genre" DataValueField="Genre" 
                        ondatabound="DropDownList1_DataBound">
            <asp:ListItem Value="%">All Genre</asp:ListItem>
        </asp:DropDownList>

                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:TESTConnectionString %>" 
                        SelectCommand="SELECT DISTINCT [Genre] FROM [Music] WHERE ([Genre] IS NOT NULL)">
                    </asp:SqlDataSource>
                </td>
            </tr>
            <tr>
                <td class="style1">
                    Album</td>
                <td>
        <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" 
                        DataSourceID="SqlDataSource2" DataTextField="Album" DataValueField="Album" 
                        ondatabound="DropDownList2_DataBound">
            <asp:ListItem Value="%">All Album</asp:ListItem>
        </asp:DropDownList>

                    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:TESTConnectionString %>" 
                        SelectCommand="SELECT DISTINCT [Album] FROM [Music] 
                        WHERE (([Album] IS NOT NULL) AND ([Genre] LIKE @Genre))">
                        <SelectParameters>
                            <asp:ControlParameter ControlID="DropDownList1" Name="Genre" 
                                PropertyName="SelectedValue" Type="String" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                </td>
            </tr>
            <tr>
                <td class="style1">
                    Title</td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"></asp:TextBox>
                </td>
            </tr>
        </table>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
            AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" 
            DataSourceID="SqlDataSource3" ForeColor="#333333" GridLines="None" Width="100%">
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <Columns>
                <asp:BoundField DataField="Genre" HeaderText="Genre" SortExpression="Genre" />
                <asp:BoundField DataField="Album" HeaderText="Album" SortExpression="Album" />
                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
            </Columns>
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#999999" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
            ConnectionString="<%$ ConnectionStrings:TESTConnectionString %>" 
            SelectCommand="SELECT Genre, Album, Title FROM Music WHERE (Album LIKE '%' + @Album + '%') 
            AND (Genre LIKE '%' + @Genre + '%') AND (Title LIKE '%' + @Title + '%')">
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownList1" Name="Genre" 
                    PropertyName="SelectedValue" Type="String" />
                <asp:ControlParameter ControlID="DropDownList2" Name="Album" 
                    PropertyName="SelectedValue" Type="String" />
                <asp:ControlParameter ControlID="TextBox1" DefaultValue="%" Name="Title" 
                    PropertyName="Text" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>

     </div>
    </form>
C#

protected void DropDownList1_DataBound(object sender, EventArgs e)
    {
        DropDownList1.Items.Insert(0, new ListItem("All Genre", "%"));
    }
protected void DropDownList2_DataBound(object sender, EventArgs e)
    {
        DropDownList2.Items.Insert(0, new ListItem("All Album", "%"));
    }