I have a ListView that I populate from the database. The ListView has a Checkbox on each row which is a built-in property of ListView.
I have been trying to get the text from the ID column next to the Checkbox which I will use in deleting the respective rows from the database but it all fails. I thought I could get the ListView.CheckedIndices and use them in return to point to the SelectedText of each index.
However all fails. More specifically i wanted to use the OnItemDeleting Event of list view but the ListView I have does not seem to have such event.
Is there any way i can achieve this?
I have had this problem before. What I did was this below:
<asp:ListView ID="xyz" runat="server">
<ItemTemplate>
<asp:LinkButton runat="server" ID="blah" OnClick="blah_Click"
CommandArgument="<%# myObject.Id %>" />
</ItemTemplate>
</asp:ListView>
I got the passed id through the command argument.Then I deleted the selected item and refreshed the data for listView. I am sure there must be a cleaner way of doing it.
Let me know how it goes.
Related
I saw few grid view examples where data is bound to grid view from a sql or other database. The question is -
<ItemTemplate>
<asp:Label ID="label1" runat="server" Text='<%#Bind("name") %>'>
</asp:Label>
</ItemTemplate>
How is Text='<%#Bind("name")%>' working? From where does the label gets the text?
I am using mysql I have a drop down list of tables, and a button. Whenever the user selects any table from ddl, and clicks on the button, I will bind the selected table with the grid.
I have enable autogenerating=true for edit and delete buttons.
I will write code for that, but whenever the user selects a different table will the grid show edit and delete buttons? and what about the Bind("value") ? will it change for every table?
Might be a silly question but please help!
The #Bind("name") command will insert the value of the column named name from whichever table you're binding to the GridView. Therefore, each of your tables would need a column named name for this label to be populated.
Also, #Bind should be used for both displaying and updating data. If you only need to display data, #Eval("name") is a better choice, as this is read-only.
you have to use the #Eval, for example,
<EditItemTemplate>
<asp:Label ID="lbleditusr" runat="server" Text='<%#Eval("Username") %>'/>
</EditItemTemplate>
using Visual.Web.Developer.2010.Express;
using SQL.Server.Management.Studio.2008.R2;
N00b here,
I've got the gridview to look the way I want it to (a textbox inside of the ItemTemplate). The Textbox's class has some client-sideJS that enables a save button (an asp:LinkButton set to look like a Jquery UI save icon) to become visible after the Textbox's .keypress event fires..
Now for my question..I've looked everywhere, but I can't get how to have gridview put the Sql server db content in that textbox on Page_load (one textbox + <br /> for each row). I'm only printing one collumn from the Sql server db into the Gridview.. Also, how would I bind the asp:LinkButton save button to gridview's save event? If there is a more effecient way to do this? If you have some insight for me, please give me your opinion/!
My .aspx code
<asp:TemplateField >
<ItemTemplate>
<asp:TextBox ID="TextBox1" class="hexen" runat="server" DataField="TbValue" SortExpression="TbValue">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:FluxConnectionString %>"
SelectCommand="SELECT [TbValue] FROM [InvestigateValues]">
</asp:SqlDataSource>
Thanks in advance!
Change your text box to
<asp:TextBox ID="TextBox1" class="hexen" runat="server" text='<%#Bind("TbValue")%>' />
This will enable two way databinding.
Here is an article to get you started: http://www.devx.com/DevX/Article/35058.
The grid view and SqlDataSource expose Insert, Update and Delete event/methods. These are on a row level, not grid level.
The way I would approach your problem would be to have an onclick event for your link button that iterates through the gridview, get the data from each text box and then perform the appropriate data base action in the code behind.
Scenarios
I am using DataGrid with a column containing DropDownList. When I update the values, the DropDownList value does not get updated.
My Research so far
I believe it needs some C# code to made the DropDownList editable. I don't know where to put the code and how? Is there a shorter way than C# code?
Thank you very much
Edit : Supplied Code
student_table : id, name, major, favorite_teacher
teacher_table : id, name
The gridview has datasource of student. The DropDownList has datasource of teacher.
In the gridview with, I have made favorite_teacher as template field (DropDownList) and has teacher_table as datasource. In the edit mode, the DropDownList shows correct and populates teachers from the teacher table. When I select a favourite teacher for any student and click submit, the submit does not go through. I might need some C# code or otherwise. Don't know how to fix this problem.
Please try the below code and let me know if it's fixed:
protected void btnClick_Click(object sender, EventArgs e)
{
var tmpValue = ((DropDownList)gvRowItem.Cells[/*cellId*/].FindControl("/*dropDownListId*/")).SelectedValue;
}
You can try the following code
DataSourceID="sds_teacher_table" is referring to the teacher_table. the gridview will be referring to the student_table
<asp:TemplateField HeaderText="Favourite Teacher" >
<ItemTemplate>
<asp:Label ID="lblfavorite_teacher" runat="server" Text='<%# LookupTeacherName(DataBinder.Eval(Container.DataItem, "id")) %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlfavorite_teacher" runat="server" DataSourceID="sds_teacher_table" DataTextField="name" DataValueField="id" SelectedValue='<%#Bind("id")%>' Width="98%">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlNewfavorite_teacher" runat="server" DataSourceID="sds_teacher_table" DataTextField="name" DataValueField="id" SelectedValue='<%#Bind("id")%>' Width="95%">
</asp:DropDownList>
</FooterTemplate>
<ItemStyle Width="25%" />
</asp:TemplateField>
protected string LookupTeacherName(object idObj)
{
if (string.IsNullOrEmpty(idObj.ToString()))
return null;
string TeacherId = idObj.ToString();
// find the corresponding name
IEnumerator enumos = sds_teacher_table.Select(new DataSourceSelectArguments()).GetEnumerator();
while (enumos.MoveNext())
{
DataRowView row = enumos.Current as DataRowView;
if ((string)row["id"].ToString() == TeacherId)
return string.Concat(row["name"].ToString());
}
return TeacherId;
}
I implemented this from a tutorial here without writing any C# code.
You do not need C# code, although you can implement it using C# code as well.
There is a trick that you have to implement. You need to bind the values of gridview to a datasource of the other table that you want to fetch data from. But by design, since dropdown list is contained in GridView, it can only be bound to gridview fields. This is the main complication.
To overcome that, include those additional fields in the gridview. You may need to use join statement so that you can include all the fields that you need in gridview. Now you can easily bind those fields to DropDownlist. Of course you do not need to show these extra values in GridView, they are there only for DropDownList. This will solve your problem, without writing a single line of code.
I declared my template as follows
<EditItemTemplate>
<asp:DropDownList ID="ddlYear" runat="server" DataSource='<%#GetYears() %>' DataTextField="year" DataValueField="year"></asp:DropDownList>
</EditItemTemplate>
I need to bind the data from the function i used GetYears() i don't know how to function it can any one help me
I need the data for example name to be loaded in dropdown when i click on Edit of gridview is it the correct way or is there any best way to do this
in the code behind you can find dropdown using findcontrol method on the click of edit link and can bind easily.
You could create an objectDatasource on the page and then set the datasourceid to that objectDataSource. Then you can create your POCO class
Look at this link for more info on objectdatasource
http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/data/objectdatasource.aspx
I have a GridView that is populated via a database, inside the GridView tags I have:
<Columns>
<asp:TemplateField>
<ItemTemplate><asp:Panel ID="bar" runat="server" /></ItemTemplate>
</TemplateField>
</Columns>
Now, I want to be able to (in the code) apply a width attribute to the "bar" panel for each row that is generated. How would I go about targeting those rows? The width attribute would be unique to each row depending on a value in the database for that row.
<asp:Panel ID="bar" runat="server" Width='<%# Eval("Width") %>' />
If you like, you can change Eval("Width") to the expression that calculates the width.
You are going to want to handle the GridView's RowCreated event. This occurs just after the GridView creates each row and will give you programmatic access to the row and all the controls contained within it.
Here is the MSDN page on the event.
I suggest you to not use Eval, if you can, because it's a little slower. In that cases I generally prefer to cast my data source to his base type:
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Panel ID="bar" runat="server" Width='<%# ((yourCustomData)Container.DataItem).Width %>' />
</ItemTemplate>
</TemplateField>
</Columns>
where yourCustomData is the row type of your data source (i.e. the element of a List<>).
This method is really faster than Eval.
Edit: oh, don't forget to include in the page a reference to the namespace that contains yourCustomData
<%# Import Namespace="yourNameSpace.Data" %>
I suggest you attach a callback handler to the OnRowDataBound event. Each row binding will fire an event which you can handle in your callback handler.
In that callback handler, you can get the info about the item being bound to the row and apply the width according to the values in your database.