I Have Menu Table and product table and MenuId ins the common field
Example
Menu Table
MenuId MenuName
11 Shirts
12 Tshirts
Product Table
ProductId ProductName MenuId ProductImage
1 Levisshirts 11 image
2 white shirt 11 image2
have display image in girdview based on drop down selection but the problem is it display same image for every products my code as follows
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
if (!IsPostBack)
ddlbind();
}
private void BindGridData()
{
SqlCommand command = new SqlCommand("SELECT * from rsa_ProductItemTable where MenuId=" + Dropsearch.SelectedItem.Value, con);
SqlDataAdapter daimages = new SqlDataAdapter(command);
DataSet ds = new DataSet();
daimages.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
GridView1.Attributes.Add("bordercolor", "black");
}
public void ddlbind()
{
SqlCommand command = new SqlCommand("SELECT * from rsa_mastermenu", con);
SqlDataAdapter daimages = new SqlDataAdapter(command);
DataTable dt = new DataTable();
daimages.Fill(dt);
Dropsearch.DataSource = dt;
Dropsearch.DataTextField = "MenuName";
Dropsearch.DataValueField = "MenuId";
Dropsearch.DataBind();
Dropsearch.Items.Insert(0, new ListItem("Select", "0"));
}
protected void Dropsearch_SelectedIndexChanged(object sender, EventArgs e)
{
int imgid = int.Parse(Dropsearch.SelectedItem.Value);
BindGridData();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Image img = (Image)e.Row.FindControl("Image1");
img.ImageUrl = "GridviewImage.ashx?ImID=" + Dropsearch.SelectedItem.Value;
}
}
What am i doing wrong please help me with this
Set image url in aspx ,
something like this
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "your_path" + "ProductImage" %>'
Height="300px" Width="300px"/>
</ItemTemplate>
Plese make sure your_path is physical path where image is stored, for example "~/images/myimg" and your image name is valid, for example image.jpg or image1.png
by this method you can show image in gridview.
Related
I have a asp page in which i have a dropdown list. The complete list of values is binded in dropdown list from the database table "a". After selecting any value from that dropdown, i save it to database table "b". Now, in 2nd asp page, i want to have that dropdown list with selected value from table "b".
My aspx page:
<asp:DropDownList DataSource='<%# getBankTable() %>' ID="ddlBankName" DataValueField='BANK_ID'
DataTextField="BANK_DESC" SelectedValue='<%# Eval("BANK_ID") %>' AppendDataBoundItems="true"
runat="server">
</asp:DropDownList>
My .cs Page:
protected void Page_Load(object sender, EventArgs e)
{
string sql1 = "SELECT * FROM Master LEFT JOIN BANK ON Master.BANK_ID = Transaction.BANK_ID";
OracleDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
ddlBankName.Items.FindByValue(dr["BANK_ID"].ToString()).Selected = true;
}
}
public void getBankTable()
{
ddlBankName.Items.Clear();
ddlBankName.Items.Insert(0, new ListItem("Select", ""));
clsDataAccess cls = new clsDataAccess();
string sql = "SELECT BANK_ID,BANK_DESC FROM Master";
DataTable dt = cls.GetDataTable(sql);
ddlBankName.DataTextField = "BANK_DESC";
ddlBankName.DataValueField = "BANK_ID";
ddlBankName.DataSource = dt;
ddlBankName.DataBind();
}
I am not able to do that. Please help!!
Personally I think it would be best to populate the Dropdown in your CS and set the value there as well.
<asp:DropDownList ID="ddlBankName" DataValueField='BANK_ID' DataTextField="BANK_DESC" AppendDataBoundItems="true" runat="server">
</asp:DropDownList>
In your CS page:
protected void Page_Load(object sender, EventArgs e) {
//Only fill it once on page load:
if (!Page.IsPostBack) {
getBankTable();
string sql1 = "SELECT * FROM Master LEFT JOIN BANK ON Master.BANK_ID = Transaction.BANK_ID";
OracleDataReader dr = cmd.ExecuteReader();
while (dr.Read()) {
if (dr["BANK_ID"] != null)
{
ddlBankName.Items.FindByValue(dr["BANK_ID"].ToString()).Selected = true;
}
}
}
}
public void getBankTable() {
ddlBankName.Items.Clear();
ddlBankName.Items.Insert(0, new ListItem("Select", ""));
clsDataAccess cls = new clsDataAccess();
string sql = "SELECT BANK_ID,BANK_DESC FROM Master";
DataTable dt = cls.GetDataTable(sql);
ddlBankName.DataTextField = "BANK_DESC";
ddlBankName.DataValueField = "BANK_ID";
ddlBankName.DataSource = dt;
ddlBankName.DataBind();
}
this is eswar.k , i have one problem in asp.net..that is ..
i have one datalist .that is shows data from database ..that is contains .check box,image,and lables..here what is the problem .. when i am checked on check box ,i have to display the email labels into the text box..(like multiple recipients eg:eswar#gmil.com,eee#yahoo.in..etc )
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
string strconnstring = System.Configuration.ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString;
string strquery = "select chid,chname,chlanguage,chrating,chemail,contenttype,data from tbl_channel_join Order by chid";
SqlCommand cmd = new SqlCommand(strquery);
SqlConnection con = new SqlConnection(strconnstring);
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
//GridView1.DataSource = dt;
//GridView1.DataBind();
//GridView2.DataSource = dt;
//GridView2.DataBind();
dl_channels.DataSource = dt;
dl_channels.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
con.Close();
sda.Dispose();
con.Dispose();
dt.Dispose();
}
Let's say you have a Gridview with checkbox like this :
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="checkIT" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
</asp:GridView>
<asp:Button ID="btnDisplay" runat="server" Text="Show data selected" OnClick="btnDisplay_Click"/>
<asp:TextBox id="textboxDataDisplay" runat="server" />
with a button to show the selected checkbox columns
C# code
protected void btnDisplay_Click(object sender, EventArgs e)
{
string data = "";
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkRow = (row.Cells[0].FindControl("chkCtrl") as CheckBox);
if (chkRow.Checked)
{
string yourFirstRowCell = row.Cells[1].Text;
string yourSecondRowCell = row.Cells[2].Text;
string yourThirdRowCell = row.Cells[3].Text;
data = yourFirstRowCell + yourSecondRowCell + yourThirdRowCell;
}
}
}
textboxDataDisplay.text = data;
}
Row cells are the cells in that row you want to get where the checkbox is checked.
Code is not working When I click edit it is not showing any text box
protected void gdvDeptDetails_RowEditing(object sender, GridViewEditEventArgs e)
{
gdvDeptDetails.EditIndex = e.NewEditIndex;
BindData();
}
public void BindData()
{
con.Open();
da = new SqlDataAdapter("select * from deptinfo",con);
ds = new DataSet();
da.Fill(ds);
gdvDeptDetails.DataSource = ds;
gdvDeptDetails.DataBind();
}
'>
Convert your grid view fields in templates and give a text box in EditItemTemplate.
Creating a list of users that haven't updated their job title in a Gridview. I want the list to have a dropdown filled with all the possible title selections and a button next to the dropdown. Then a person can come in and change the title in the dropdown hit the button and its updated and removed from the list.
I have all of this the way I want it to look but I'm trying to figure out how to pass the SelectedValue of the dropdown box in that row to the code behind OnClick. As you can see below the closest I can get is pass the row number in the CommandArgument. Any suggestions how I can get the SelectedValue of the dropdown of that specific row to the OnClick?
EDIT: Maybe I should be using OnRowCommand instead of OnClick?
Looks like this currently:
John Doe | DropdownList Button
Jane Doe | DropdownList Button
Joe Doe | DropdownList Button
Jeff Doe | DropdownList Button
ASPX
<asp:GridView runat="server" ID="TitleView" OnRowDataBound="TitleView_RowDataBound" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Fullname" HeaderText="Fullname" />
<asp:TemplateField>
<ItemTemplate>
<div class="input-append"><asp:DropDownList CssClass="span5" ID="TitleList" runat="server">
</asp:DropDownList>
<asp:Button ID="lbnView" runat="server" Text="Update" CssClass="btn btn-primary" OnClick="btn_Clicked"
CommandArgument='<%# ((GridViewRow)Container).RowIndex %>'></asp:Button></div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code Behind
public void bindTitleView()
{
using (SqlConnection conn = new SqlConnection(""))
{
SqlCommand cmd = new SqlCommand(#"SELECT U.First + ' ' + U.Last as Fullname, U.UserID, T.Name FROM Employees U LEFT JOIN Titles T ON U.Title = T.ID WHERE U.Active = '1' AND U.Title = '92' ORDER BY Fullname ASC", conn);
conn.Open();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet myDataSet = new DataSet();
adp.Fill(myDataSet);
TitleView.DataSource = myDataSet;
TitleView.DataBind();
}
}
protected void TitleView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("TitleList");
using (SqlConnection conn = new SqlConnection(""))
{
SqlCommand cmd = new SqlCommand(#"SELECT ID, Name FROM Titles ORDER BY Name ASC", conn);
conn.Open();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataTable myDataSet = new DataTable();
adp.Fill(myDataSet);
ddl.DataSource = myDataSet;
ddl.DataTextField = "Name";
ddl.DataValueField = "ID";
ddl.DataBind();
}
}
}
protected void btn_Clicked(object sender, EventArgs e)
{
String rowid = ((Button)sender).CommandArgument;
}
SOLUTION: The answer I approved below worked for me once I added !IsPostBack to the Page_Load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindTitleView();
}
You can do like that:
protected void btn_Clicked(object sender, EventArgs e)
{
int line = ((GridViewRow)((Button)sender).Parent.Parent).RowIndex;
DropDownList drp = ((DropDownList)TitleView.Rows[line].FindControl("TitleList"));
//Continue the method
}
In your btn_click write the following code
protected void btn_Clicked(object sender, EventArgs e)
{
Button Sample = sender as Button;
GridViewRow row = Sample.NamingContainer as GridViewRow;
DropDownList drp = row.FindControl("TitleList") as DropDownList;
//Now use drp.SelectedValue
}
}
Let me know if this isnt what you are looking for.
I have a GridView containing data extracted from two TextBoxes on click of a button. I want the following functionalities to be implemented in the Gridview:
1) I want to be able to Edit the data in this GridView.
2) I should also be able to Delete the rows from the GridView.
3) Finally, when I click on another Submit button, all the rows from the Gridview should be saved in the database.
Its a web-based ASP.NET application coded using C# (Visual Studio 2010), and uses SQL Server 2005. How can I make changes to the below code to implement the above specified functionality?
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["constring"]);
SqlCommand cmd = new SqlCommand();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
dt = Session["data_table"] as DataTable;
}
}
protected void btnTextDisplay_Click(object sender, EventArgs e)
{
if (dt == null)
{
dt = new DataTable();
DataColumn dc1 = new DataColumn("Name");
DataColumn dc2 = new DataColumn("City");
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
Session["data_table"] = dt;
}
DataRow dr = dt.NewRow();
dr[0] = txtName.Text;
dr[1] = txtCity.Text;
dt.Rows.Add(dr);
gvDisplay.DataSource = dt;
gvDisplay.DataBind();
}
protected void btnDisplay_Click(object sender, EventArgs e)
{
ds.Clear();
da = new SqlDataAdapter("insert into PRACT values(#name, #city)", con);
con.Open();
da.Fill(ds);
gv.DataSource = ds;
gv.DataBind();
con.Close();
}
}
Well on your aspx page I would recommend you to do that:
<asp:GridView runat="server" id="gvDisplay" OnRowCommand="grid_OnRowCommand">
<Columns>
<TemplateField>
<ItemTemplate>
<asp:TextBox runat="server" id="txtNameGrid" Text='<%#DataBinder.Eval(Container.DataItem, "Name")%>'/>
</ItemTemplate>
</TemplateField>
<TemplateField>
<ItemTemplate>
<asp:TextBox runat="server" id="txtCityGrid" Text='<%#DataBinder.Eval(Container.DataItem, "City")%>'/>
</ItemTemplate>
</TemplateField>
<TemplateField>
<ItemTemplate>
<asp:Button runat="server" id="btnDeleteGrid" Text = "Delete" CommandArgument='<%#Eval(Container.DataItem, "YourIDColumn")%>' CommandName="DeleteRow"/>
</ItemTemplate>
</TemplateField>
</Columns>
</asp:GridView>
I recommend you to create a new column in your DataTable, this column will be the ID of each register.
Well, you add registers to this DataTable on your page, so you will need to create a session of type int and each time the event btnTextDisplay_Click is called you must increase this int Session and set it's value to the DataTable's Column ID.
The grid's attribute, OnRowCommand, is the event that will be called when you click on the button btnDeleteGrid. The code of this event comes below:
protected void grid_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName == "DeleteRow")
{
foreach(DataRow row in dt.Rows)
{
if(Convert.ToInt32(row["YourColumnID"]) == Convert.ToInt32(e.CommandArgument))
row.Delete();
}
dt.AcceptChanges();
gvDisplay.DataSource = dt;
gvDisplay.DataBind();
}
}
Your event that will save the registers should be like that.
protected void btnSave_Click(object sender, EventArgs e)
{
foreach(DataRow row in dt.Rows)
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["constring"]);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "INSERT INTO YOUR_TABLE_NAME (NAME, CITY) VALUES (" + row["Name"].ToString() + "," + row["City"].ToString() + ")";
int numRegs = cmd.ExecuteNonQuery();
}
}
I really expect I helped.
I can't test my code and I'm not so good on work with DataTables, so if there's any problem with my code, just let me know.
I think it will be more efficient if you use Bulk Inserts to realize the inserts. Search for how to make it, it's pretty cool and quick.
And also try to use Stored Procedures, because it's safer than use direct command texts. Using them will prevent SQL Injection.
Best regards.