Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi..i have one question

how to set two images according to condtion in gridviw in asp.net


i want when i click on the result button then correct answer and user answer status will be displayed..if user answe and correct answer is true then first image displayed otherwise second image will b displayed


result page coding:
BL.querystudent objquery = new BL.querystudent(ConfigurationManager.ConnectionStrings["reetu"].ToString());
static DataTable dt = new DataTable();
static DataTable dtNew = new DataTable();
static string pname;
static double result,marks;
static int marks1, attempt,unattempt,correct,incorrect;
static int eid, stid,pid,sid;
string a, b;

protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
Label1.Text = Session["Email"].ToString();
string no = Session["no"].ToString();
string name = Session["examname"].ToString();
// pname = Session["programname"].ToString();
dt = objquery.getexamresult(name);
int eid = Convert.ToInt32(dt.Rows[0]["ExamID"]);
string stname = Label1.Text;
dt = objquery.getsid(stname);
int stid = Convert.ToInt32(dt.Rows[0]["StudentID"]);
string started = Session["start"].ToString();
dt = objquery.getresult(eid,stid);

foreach (DataRow dr in dt.Rows)
{
a = dr["UserAnswer"].ToString();
b = dr["CorrectAnswer"].ToString();

if ((a != "") && (a == b))
{
attempt += 1;
correct += 1;
marks += 1;
}
else if ((a != "") && (a != b))
{
attempt += 1;
incorrect += 1;
marks -= 0.25;
}
else if (a == "")
{
unattempt += 1;
}
}


int n = Convert.ToInt32(no);
result = Convert.ToDouble((marks * 100) / n);
TextBox6.Text = no.ToString();
TextBox7.Text = attempt.ToString();
TextBox8.Text = unattempt.ToString();
TextBox1.Text = correct.ToString();
TextBox2.Text = incorrect.ToString();
TextBox9.Text = result.ToString();
attempt = 0;
unattempt = 0;
marks = 0;
marks1 = 0;
correct = 0;
incorrect = 0;



}
}


templatefield coding:

XML
<asp:TemplateField HeaderText="Status">
          <ItemTemplate>
              <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Status")=="true" ? "~/pics/login-btn.png" :  "~/pics/check.jpg"%>' />

          </ItemTemplate>
          </asp:TemplateField>






gridview coding:

BL.querystudent objquery = new BL.querystudent(ConfigurationManager.ConnectionStrings["reetu"].ToString());
static DataTable dt = new DataTable();
static DataTable dtNew = new DataTable();
static int stid;
static string stname,tknid;
static bool status;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Label1.Text = Session["Email"].ToString();
stname = Label1.Text;
tknid = Session["token"].ToString();
dtNew = objquery.getsid(stname);
stid = Convert.ToInt32(dtNew.Rows[0]["StudentID"]);
bindtogrid();
}
}

public void bindtogrid()
{
dt = objquery.getsturesult(tknid, stid);


GridView1.DataSource = dt;
GridView1.DataBind();
}

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{


if (dt.Rows.Count > 0)
{
bindtogrid();
}


}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
dt=objquery.getstatus(tknid,stid);


}
}
Posted

1 solution

u can check ur condition on GridView RowDataBound Event


C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

    {

       Label lblID = (Label)e.Row.FindControl("Your_lblIDName"); //find control inside GridView

       Image img = (Image)e.Row.FindControl("Your_imgName"); //find control inside GridView

        if (lblID != null)

        {

            if (lblID.Text == "2" || lblID.Text == "3")

                img.ImageUrl = "Set ur image url here.";

            else

                img.Visible = false;

        }

    }





and you can try this link for the grid view tips and tricks, here they explain about how to change the color of the grid view depends on condition, you can use the trick for images

http://www.dotnetcurry.com/ShowArticle.aspx?ID=172[^]
 
Share this answer
 
v3

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