Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm stuck in a very irritating problem. I tried this thing two months earlier also but had to change the approach after failure as I was in a hurry to complete the project. Now I'm in a vacation and want to solve the problem. I'm fed up with this. I'eve tried from many sources on the web. Most of the sources refer to the procedure I pasted below, but that doesn't work in my code. I wanna know what's wrong. :( Please please please help!!

the .aspx code is here :


C#
<%@ Page Title="" Language="C#" MasterPageFile="~/SitePages/Master_LoggedIn.master" AutoEventWireup="true" 

CodeFile="ChooseExam.aspx.cs" Inherits="ChooseExam" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">

<asp:DataList ID="DataList1" runat="server" RepeatColumns="4" OnItemCommand="DataList1_ItemCommand" 

RepeatDirection="Horizontal" Width="100%">
            <HeaderTemplate>
                <div style="text-align: center; height: 38px; background-color: #FFFFCC">
                    <span style="font-family: 'Bookman Old Style'; color: #003300">Click On 
                    The Test You Want To Take</span></div>
            </HeaderTemplate>
            <ItemTemplate>
                <table class="style1">
                    <tr>
                        <td>
                            <asp:LinkButton ID="LinkButton_ExamName" runat="server" BorderColor="Black"  
                                
                                style="font-family: 'Bookman Old Style'; color: #000000; text-decoration: none;"><%#Eval

("ExamName")%></asp:LinkButton>
                                
                        </td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:DataList>

<p>
The Chosen Exam is <asp:Label ID="Label_ExamName" runat="server" 
                                style="font-family: 'Bookman Old Style'"></asp:Label>
</p>

</asp:Content>


and here's the .aspx.cs code:


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class ChooseExam : System.Web.UI.Page
{
    ClassOnlineExamConnection con = new ClassOnlineExamConnection();
    DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
        Label_ExamName.Visible = false;
        ds = new DataSet();
        ds = con.dataFetch("Select ExamCode, ExamName From Exams");

        DataList1.DataSource = ds;
        DataList1.DataBind();
    }

    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        DataList1.SelectedIndex = e.Item.ItemIndex;
        String A = ((LinkButton)e.Item.FindControl("LinkButton_ExamName")).Text;
        Label_ExamName.Text = A;
        Label_ExamName.Visible = true;
    }
}


thank a lot in advance. I'll be grateful to you..
Posted

1 solution

don't bind data in each post back, do as below
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Label_ExamName.Visible = false;
        ds = new DataSet();
        ds = con.dataFetch("Select ExamCode, ExamName From Exams");
    
        DataList1.DataSource = ds;
        DataList1.DataBind();
    }
}

Also change your link button as below
<asp:LinkButton ID="LinkButton_ExamName" CommandName="Select" CommandArgument='<%#Eval("ExamName")%>' runat="server" BorderColor="Black" tyle="font-family: 'Bookman Old Style'; color: #000000; text-decoration: none;"><%#Eval("ExamName")%></asp:LinkButton>


then

C#
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
    if(e.CommandName ="Select")
    {
        var e = e.CommandArgument.ToString();
        Label_ExamName.Text = e;
        Label_ExamName.Visible = true;
    }
}
 
Share this answer
 
v2
Comments
Nirav Prabtani 24-Jun-14 23:54pm    
my 5+
DamithSL 25-Jun-14 0:15am    
Thank You Nirav

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900