loading multiple images attachecd to one thumbnail - c#

Hi coder i search a lot but didn't find what i want
i have a database in which i have path of my images.. now i bind images with by gridview and apply lightbox on it but what i want is to show only one image in front as thumbnail and when i click on that images slide show will start..
this is my code:
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"
AutoGenerateColumns="False" PageSize="1" PersistedSelection="true" DatakeyNames="pptId">
<Columns>
<asp:BoundField headerText="pptId" DataField="pptId"></asp:BoundField>
<asp:TemplateField HeaderText="View PPT">
<ItemTemplate>
<a id="imageLink" href='<%# Eval("Imageurl") %>' title='<%#Eval("Description") %>'
rel="lightbox[Brussels]" runat="server">Show PPT</a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
<SortedAscendingCellStyle BackColor="#F5F7FB"></SortedAscendingCellStyle>
<SortedAscendingHeaderStyle BackColor="#6D95E1"></SortedAscendingHeaderStyle>
<SortedDescendingCellStyle BackColor="#E9EBEF"></SortedDescendingCellStyle>
<SortedDescendingHeaderStyle BackColor="#4870BE"></SortedDescendingHeaderStyle>
</asp:GridView>
this is my aspx.cs page code:
protected void Page_Load(object sender, EventArgs e)
{
BindModule();
}
protected void BindModule()
{
try
{
con.Open();
string id = Request.QueryString["pptId"].ToString();
//Query to get ImagesName and Description from database
SqlCommand command = new SqlCommand("select * from Image_Master where pptId='" + id + "' and IsEnable='True' order by Priority", con);
dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(command);
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
GridView1.Dispose();
}
catch (Exception ex)
{
Response.Write("Error occured : " + ex.Message.ToString());
}
finally
{
con.Close();
}
problem is that when i bind the gridview it will show all images regarding selected pptid and when i click on any images my sildeshow start but what i want is to show only one image and when i click on that image my slide show start. i use lightbox for slide show and my images comes from database

Related

Binding Information to the GridView from SQLServer won't work in C#

I have a form where I upload documents to Gridview and then save to the database.
When I want to load the form, I can't get the document info loaded to my GridView. I have put even an AlertMessage to know if it reads the files and it does. So I get the Message "Table is not empty" when loading the file. I don't know what else, I have to do in order to load the info to the grid.
Please, help me with it. Here is my code:
<asp:GridView ID="gvUploadDocumentList" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None"
BorderWidth="1px" CellPadding="4" EmptyDataText="No files uploaded"
ForeColor="Black" GridLines="Horizontal" style="text-align: center;
margin:0px auto;" Width="800px">
<Columns>
<asp:BoundField DataField="Filename" HeaderText="File Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkDelete" runat="server" CommandArgument='<%# Eval("ID") %>' Text="Delete" OnClick="DeleteFile" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#4B4B4B" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#242121" />
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if(!page.IsPostBack)
{
LoadRequestInfo();
LoadDocuments();
}
}
public void LoadDocuments()
{
CommonCollection c = new CommonCollection();
var CTS = c.COMPTRACKERCONNECTIONSTRING();
var CTS_conn = new SqlConnection(CTS.ConnectionString);
try
{
SqlCommand cmd = new SqlCommand("CTS_GetDocuments", CTS_conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("#RequestId", "1F0C0732-34CD-48DC-9730-268FE17BAF03"));
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
CTS_conn.Open();
da.Fill(ds);
CTS_conn.Close();
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
gvUploadDocumentList.DataSource = ds;
gvUploadDocumentList.DataBind();
ShowAlertMessage("Table is not empty");
}
else
ShowAlertMessage("No Data");
}
catch (Exception ex)
{
ShowAlertMessage(ex.Message.ToString());
}
}
Just based on what you are listing, I don't see any code that calls the LoadDocuments() method. If this isn't called in the PageLoad() method, and there is no button or other even that calls it, you will never get the data into your gridview.
An easy way to verify if this is happening is to put a break into your code at the beginning of the method and if VS doesn't start stepping through your code, that's your issue.

ASP.NET - i'm looking for a way to change a subString font into a gridview cell

<asp:TextBox ID="searchDescription" runat="server" Height="26px" Width="270px"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button2" runat="server" Text="Търси" onclick="Button2_Click" />
<br />
<br />
So here is my gridview :
<asp:GridView ID="GridView2" runat="server" HorizontalAlign="Center" AllowPaging="True"
AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="Марка" HeaderText="Марка" SortExpression="Марка" />
<asp:BoundField DataField="Година" HeaderText="Година"
SortExpression="Година" />
<asp:BoundField DataField="Мощност" HeaderText="Мощност"
SortExpression="Мощност" />
<asp:BoundField DataField="Вносител" HeaderText="Вносител"
SortExpression="Вносител" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
I have a description text field and DropDownList and a button which generates a query that takes out from the DB * from the table where one of the column is equal to the selected Item from the DropDown and if in the description cell contains the subString from the description text field. So i want when the description cell appears into the gridView, the subString from the text field to be bolded.
Here is my button method:
protected void Button2_Click(object sender, EventArgs e)
{
string srchDescription = searchDescription.Text;
string slctItem = DropDownList1.SelectedItem.ToString();
con.Open();
SqlCommand cmdForDescription = new SqlCommand("SELECT Description FROM car_table WHERE Вносител = '" + slctItem + "'", con);
string descriptionText = cmdForDescription.ExecuteScalar().ToString();
bool containsDescription = descriptionText.Contains(srchDescription);
if (containsDescription)
{
SqlCommand cmd = new SqlCommand("SELECT * FROM car_table WHERE Вносител = '" + slctItem + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView2.DataSource = ds;
GridView2.DataBind();
con.Close();
Label2.Visible = true;
}
}
First set htmlencode="false" on your bound field like this.
<asp:BoundField DataField="Description" HeaderText="Description"
htmlencode="false" SortExpression="Description" />
The in RowDataBound of your gridview write following.
if(e.Row.RowType == DataControlRowType.DataRow)
{
string textToReplace = searchDescription.Text;
string newText = "<b>" +searchDescription.Text + "</b>";
e.Row.Cells[4].Text =e.Row.Cells[4].Text.Replace(textToReplace, newText);
}

By using below code how can i delete image from folder in asp .net

<asp:GridView ID="gvDisplayImages" runat="server" BackColor="#CCCCCC"
BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4"
CellSpacing="2" ForeColor="Black" AutoGenerateColumns="false" DataKeyNames="intId"
onrowdeleting="gvDisplayImages_RowDeleting"
onselectedindexchanged="gvDisplayImages_SelectedIndexChanged">
<Columns>
<asp:CommandField SelectText="Delete" ShowDeleteButton="true" />
<asp:BoundField DataField="varImageName" HeaderText="ImageName"
SortExpression="varImageName" />
<asp:TemplateField HeaderText="Preview Image">
<ItemTemplate>
<asp:Image ID ="Img1" runat="server" Height="150" Width="200" ImageUrl='<%#ResolveUrl ("~/" + Eval("varImagePath")) %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<RowStyle BackColor="White" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
code for cs page
protected void Page_Load(object sender, EventArgs e)
{
BindData();
}
public void BindData()
{
gvDisplayImages.DataSource = bcObj.DisplayImages();
gvDisplayImages.DataBind();
}
protected void gvDisplayImages_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int intId = Convert.ToInt32(gvDisplayImages.DataKeys[e.RowIndex].Value.ToString());
bcObj.DeleteImage(intId);
BindData();
string filename = Convert.ToString(gvDisplayImages.Rows[e.RowIndex].FindControl("Img1") as Image);
string filepath = "\\Image\\" + filename;
FileInfo file = new FileInfo(filepath);
if (file.Exists)
{
file.Delete();
}
}
Here I am using 3 tier architecture in asp .net By using above code while click delete button in gridview, images are deleted from database successfully. Now please tell me how should I delete image from folder where all images are stored.
Need to use ImageUrl in your code.
like this
string filename = ((Image)(gvDisplayImages.Rows[e.RowIndex].FindControl("Img1"))).ImageUrl;

How to hide and show the DetailsView in the Master-Details GridView & DetailsView?

I am trying to follow and utilize the explained example in ASP.NET website that is about Quiz Engine. I have a Master-Details in the result page where when the user selects one of his answered quesitons in the GridView, the details of that question will be displayed in the DetailsView underneath the GridView. Everything works fine. What I want now is just making the DetailsView (which is the details of the answered question) hidden unless the user selects one of the answered questions. So how to do that?
I set visibility of the DetailsView to false, but I don't know how to hide/show based on the user click on the SELECT option.
My ASP.NET code:
<tr>
<td>
<asp:GridView ID="resultGrid" runat="server" DataKeyNames="QuestionID" SelectedIndex="0"
AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateSelectButton="True" OnSelectedIndexChanged="resultGrid_SelectedIndexChanged" Width="555px">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" CssClass="generaltext" HorizontalAlign="Center" />
<Columns>
<asp:BoundField DataField="QuestionID" HeaderText="Question" />
<%--<asp:BoundField DataField="CorrectAnswer" HeaderText="Correct Answer" />--%>
<asp:BoundField DataField="UserAnswer" HeaderText="Your Answer" />
<asp:BoundField DataField="Result" HeaderText="Result" />
</Columns>
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" CssClass="boldtext" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT [Question], [Answer1], [Answer2], [Answer3], [QuestionID], [QuestionOrder], [Answer4], [CorrectAnswer], [AnswerExplanation], [QuizID] FROM [Question] WHERE ([QuizID] = #QuizID) ORDER BY [QuestionOrder]">
<SelectParameters>
<asp:SessionParameter Name="QuizID" SessionField="QuizID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</td>
</tr>
<tr>
<td>
<asp:DetailsView ID="answerDetails" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" Height="45px" Width="552px" DataSourceID="SqlDataSource1"
AutoGenerateRows="False" DataKeyNames="QuestionID" Visible="false">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" CssClass="generaltext" />
<FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" CssClass="boldtext" Width="100px" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Fields>
<asp:BoundField DataField="Question" HeaderText="Question"
SortExpression="Question" />
<asp:BoundField DataField="Answer1" HeaderText="A"
SortExpression="Answer1" />
<asp:BoundField DataField="Answer2" HeaderText="B"
SortExpression="Answer2" />
<asp:BoundField DataField="Answer3" HeaderText="C"
SortExpression="Answer3" />
<asp:BoundField DataField="Answer4" HeaderText="D"
SortExpression="Answer4" />
<asp:BoundField DataField="CorrectAnswer" HeaderText="Correct Answer"
SortExpression="CorrectAnswer" HeaderStyle-BackColor="lightgreen" />
<asp:BoundField DataField="AnswerExplanation" HeaderText="Explanation"
SortExpression="AnswerExplanation" HeaderStyle-BackColor="lightgreen" />
</Fields>
</asp:DetailsView>
</td>
</tr>
Code-Behind code:
public partial class Results : System.Web.UI.Page
{
Object bShowDetailsView;
protected void Page_Load(object sender, EventArgs e)
{
bShowDetailsView = false;
ArrayList al = (ArrayList)Session["AnswerList"];
if (al == null)
{
Response.Redirect("default.aspx");
}
resultGrid.DataSource = al;
resultGrid.DataBind();
// Save the results into the database.
if (IsPostBack == false)
{
// Calculate score
double questions = al.Count;
double correct = 0.0;
for (int i = 0; i < al.Count; i++)
{
Answer a = (Answer)al[i];
if (a.Result == Answer.ResultValue.Correct)
correct++;
}
double score = (correct / questions) * 100;
string username = HttpContext.Current.User.Identity.Name.ToString().Replace("ARAMCO\\", "");
SqlDataSource userQuizDataSource = new SqlDataSource();
userQuizDataSource.ConnectionString = ConfigurationManager.ConnectionStrings["testConnectionString"].ToString();
userQuizDataSource.InsertCommand = "INSERT INTO [UserQuiz] ([QuizID], [DateTimeComplete], [Score], [Username]) VALUES (#QuizID, #DateTimeComplete, #Score, #Username)";
userQuizDataSource.InsertParameters.Add("QuizID", Session["QuizID"].ToString());
userQuizDataSource.InsertParameters.Add("DateTimeComplete", DateTime.Now.ToString());
// "N4" is for displaying four decimal places, regardless of what the value is
userQuizDataSource.InsertParameters.Add("Score", score.ToString("N4"));
userQuizDataSource.InsertParameters.Add("Username", username);
int rowsAffected = userQuizDataSource.Insert();
if (rowsAffected == 0)
{
// Let's just notify that the insertion didn't
// work, but let' s continue on ...
errorLabel.Text = "There was a problem saving your quiz results into our database. Therefore, the results from this quiz will not be displayed on the list on the main menu.";
}
}
}
protected void resultGrid_SelectedIndexChanged(object sender, EventArgs e)
{
SqlDataSource1.FilterExpression = "QuestionOrder=" + resultGrid.SelectedValue;
bShowDetailsView = true;
answerDetails.Visible = bShowDetailsView;
}
}
why not make the first record in resultGrid selected by default because the event you are handling is SelectedIndexChanged but not Selected/Unselected (i dont know whether these are there or not)

Paging in gridview

My gridView:
<asp:GridView ID="gridView1" runat="server" CellPadding="4" AllowPaging="true" PageSize="5" emptydatatext="No data available."
CssClass="datagrid"
ForeColor="#333333" GridLines="None"
onrowcreated="gridView1_RowCreated"
onpageindexchanging="gridView1_PageIndexChanging">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
And my codebehind:
protected void gridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gridView1.PageIndex = e.NewPageIndex;
BindDataGrid();
}
protected void BindDataGrid()
{
DataSet ds = new DataSet();
ds = dbM.GetInfo(name, mobilePhone, info1); //get info to BD and save in "ds"
gridView1.DataSource = ds;
gridView1.DataBind();
}
and the "Paging" does not work. It shows the first 5 rows, but does not show the number of paging
What´s wrong???
Please help
thanks
Try adding some pager settings in the GridView tag:
<PagerSettings Mode="NextPreviousFirstLast" Position="TopAndBottom" />
If that works, you can change it to your desired appearance. There are many choices, including
FirstPageImageUrl
FirstPageText
LastPageImageUrl
LastPageText
Mode (such as Numeric or
NumericFirstLast)
NextPageImageUrl
NextPageText
PageButtonCount
Position
PreviousPageImageUrl
PreviousPageText
Visible`
Add the PagerSettings-Property to the GridView-Markup and set it to True:
PagerSettings-Visible="true"
Edit: but it should be true by default, so i'm not sure if this will change anything
Use **"PageIndexChanged"**
protected void gridView1_PageIndexChanged(object sender, GridViewPageEventArgs e)
{
try
{
gridView1.PageIndex = e.NewPageIndex;
BindDataGrid();
;
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}

Categories