Site Search

Thursday, June 30, 2011

How to Pass value in to code behind when click on Gridview row - Server side

This will show how to pass key values in to code behind when you click on GridView row link button.

You have to use:
·         Command Name
·         OnRowCommand Event
·         TemplateField with Link button.
ASPX
·         Add row command in to gridview
·         OnRowCommand="GridViewRowCommand"
·         Add template field with asp link button and set the command name as you wish.put the data table column name you want to bind in to the Text ad evalue.
                <asp:TemplateField>
                    <HeaderStyle/>
                    <ItemTemplate>
                        <asp:LinkButton ID="Number" runat="server" Text='<%# Eval("Number") %>' CommandName="Number"
                            CommandArgument='<%# Eval("Number") %>'></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>

ASPX.CS
·         At the row command event capture the value set by aspx and do your work..
protected void GridViewRowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName != null)
            {
                if (e.CommandName == "Number")
                {
                     string Number = e.CommandSource.ToString();
                     //Your code
                }
            }

        }
cheers!

How to add image link in to Gridview column

This will add cursor hand when hover on to the image.
.aspx
              <Columns>
                <asp:TemplateField>
                    <HeaderStyle HorizontalAlign="Left" Width="15px" />
                    <ItemTemplate>
                        <asp:HyperLink ID="Image" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
aspx.cs
protected void GridViewRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {         
                HyperLink rLink = (HyperLink)e.Row.Cells[column index].Controls[1];
                Link.ImageUrl = "image path";
                Link.NavigateUrl = "http://www.google.com";
            }
        }

How to remove Gridview sort column Header underline - for beginners(C#)

code behind:
Use fallowing method in RowDatabound Event.
RemoveHeaderUnderLine(e);
        private static void RemoveHeaderUnderLine(GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                for (int cell = 0; cell < e.Row.Cells.Count; cell++)
                {
                    if (e.Row.Cells[cell].Controls.Count > 0)
                    {
                        LinkButton Link = (LinkButton)e.Row.Cells[cell].Controls[0];
                        Link.Attributes.Add("style", "text-decoration:none;");
                    }
                }
            }
        }

Creating a DataTable - for beginners(C#)

How to create a DataTable for bind Data

private static DataTable CreateDataTable()
        {
            DataTable table = new DataTable();
            table.Columns.Add(AccountNumb, typeof(Int64));
            table.Columns.Add(Name, typeof(string));
            table.Columns.Add(Owner Address, typeof(string));

            table.Rows.Add("001225", "Gustavo Santaolalla", " frederick burner");
            table.Rows.Add("001220", "Gustavo Santaolalla", " frederick burner");
            return table;

        }

Get started DNN - DNN for dummies

CREATING A DNN WEB SITE
Prerequisite:
§ DDN Zip file(Required version ex:5.06.02)
§ DNN installation(5.6.2 Visual Studio starter kit)
§ MS SQL Express.
Standard way
 1.Creat a class library project.
 2.Install the Visual studio starter kit.
 3.Extract the DNN Zip file and copy it inside to the physical location of the created class library project is.
 4.Add the DNN location/Site as an existing web site to the project created early.
 5.Then run the site if success it will automatically add the DB in to SQL Server.
Way -2
 1.Change the configuration file by changing the Database.
 2.Then run the site it will install the site. with adding required tables in to DB
IIS configurations
 1.Create a virtual directory in IIS
 2.Give the Physical path to the DNN directory in the project.
 3.Giv "Network Services " Privileges to the DNN directory.

When does the Database.mdf crated in DNN

this is important point that I'm used to forget ;D so I wanted to get it noted somewhere so that the reason of this post and think this might help some others as well.

• in DNN the database.mdf will created at the point of it going to install.
• In my case it will created when you build by using NANT in command line then after you can attach it.

Server Error in '/' Application



A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)



Fix- Go to Local service -start sql service  :)

StringSplit function -SQL

This is a function used to split string and fie sql queries
ALTER PROCEDURE [Door].[XXXXXXXXXXXXXXXXXXXXXXXXXXx]
@GroupIdList varchar(max)
,@Type int
AS
BEGIN
DECLARE @Delimeter CHAR(1);

Fire javascript function by code behind

You can access JavaScript function by code behind and fire those easily.
Server side
string myScript = "close();";
ScriptManager.RegisterStartupScript(this.Page, typeof(Page),"CloseWindowKey", myScript, true);
Client side
function close() {
//Functionality for close pop up window
}

Adding Image button to data grid view

1.Add image button under template field it will be easier than adding a command field.
<Columns>
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
<HeaderStyle HorizontalAlign="center" Width="35px" />
<ItemTemplate>
<asp:ImageButton ID="selectButton" CommandName="Select" runat="server"/>
ItemTemplate>
asp:TemplateField>
<asp:BoundField DataField="Name">
<HeaderStyle HorizontalAlign="Left" />
asp:BoundField>
<asp:BoundField DataField="Id">
<HeaderStyle HorizontalAlign="Left" />
asp:BoundField>
<asp:BoundField DataField="MainCreditorName">
<HeaderStyle HorizontalAlign="Left" />
asp:BoundField>
Columns>
2.Add the SelectedIndexChanged event and it will fire when you click on image button on the grid.
protected void XXXXXXXXXX_SelectedIndexChanged(object sender, EventArgs e)
{
}
3.You are done

Server side Row Click - Get data from GridView

ASPX Page

<asp:GridView ID="GridViewUsers" runat="server" AutoGenerateColumns="False"Width="100%"
OnSelectedIndexChanged="GridViewUsers_SelectedIndexChanged"DataKeyNames="Id" OnRowCommand="GridViewUsers_RowCommand">
<Columns>
<asp:BoundField HeaderText="User Name" DataField="Name" />
<asp:BoundField HeaderText="Main Creditor" DataField="Id" />
<asp:BoundField HeaderText="Main CReditorName" DataField="MainCreditorName"/>
Columns>
<HeaderStyle HorizontalAlign="Left" CssClass="spn_gridHeader" />
<%-- --%>
asp:GridView>


CS
protected void GridViewUsers_SelectedIndexChanged(object sender, EventArgse)
{
}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
foreach (GridViewRow row in GridViewUsers.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
ClientScriptManager cs = Page.ClientScript;
row.Attributes["onmouseover"] =
"this.style.cursor='hand';this.style.textDecoration='underline';";
row.Attributes["onmouseout"] =
"this.style.textDecoration='none';";
// Set the last parameter to True
// to register for event validation.
row.Attributes["onclick"] =
cs.GetPostBackClientHyperlink(GridViewUsers,
"Select$" + row.DataItemIndex, true);
}
}
base.Render(writer);
}


Page Load
GridViewUsers.RowCommand += GridViewUsers_RowCommand;