|
I have a markup page that was using CommandField delete button. This was later replaced with regular button control.
Code:
<table>
<tr>
<td>
<asp:gridview ID="Gridview1" RowStyle-Wrap="false" gridlines="None" CssClass="responsiveTable1" runat="server" ShowFooter="true" AutoGenerateColumns="false" OnRowDeleting="Gridview1_RowDeleting">
<Columns>
<asp:BoundField DataField="RowNumber" Visible="false" HeaderText="Row Number" />
<asp:TemplateField HeaderText="Name">
<headerstyle horizontalalign="Left" />
<ItemTemplate>
<asp:TextBox ID="txtsourcename" placeholder="Name...(e.g, Jane Doe)" runat="server" style="width:375px;" AutoPostBack="true" class="form-control textClass" OnTextChanged="txtsourcename_TextChanged"></asp:TextBox><br />
<asp:CheckBox ID="grid1Details" ClientIDMode="Static" runat="server" Checked="false" AutoPostBack="true" OnCheckedChanged="Grid1CheckChanged" /><span style="color:#ff0000">*Check this box if N/A</span>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemStyle HorizontalAlign="Left"></ItemStyle>
<ItemTemplate>
<asp:TextBox ID="txtsourceaddress" placeholder="Address..." runat="server" style="width:375px;" class="form-control textClass"></asp:TextBox><br /><br />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add"
onclick="ButtonAdd_Click" CssClass="grvAddButton" /><br /><br /><br>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button ID="sourceDelete" runat="server" Text="Delete" CommandName="Delete"
CssClass="grvDelButton" OnClientClick="return confirm('Are you sure you want to remove this row?')" /> <br /><br /><br />
</ItemTemplate>
</asp:TemplateField>
<%-- <asp:CommandField ShowDeleteButton="True"><ControlStyle CssClass="grvDelButton" /></asp:CommandField>--%>
</Columns>
</asp:gridview>
</td>
</tr>
</table>
The delete button is used to delete any additional / unwanted rows created in dynamically generated row in GridView.
Since the commandField delete button was replaced, the VB the engineers the delete action is now throwing an error:
Unable to cast object of type 'System.Web.UI.WebControls.TemplateField' to type 'System.Web.UI.WebControls.CommandField'
Does anyone know how to modify this to reflect the button control ID used in markup instead of CommandField?
Protected Sub GrvSource_RowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.Header Then
If e.Row.RowIndex = -1 Then
DirectCast(DirectCast(e.Row.Cells(4), DataControlFieldCell).ContainingField, CommandField).ShowDeleteButton = False
End If
Else
If Convert.ToInt32(ViewState("rowIndex" & 0)) = e.Row.RowIndex Then
DirectCast(DirectCast(e.Row.Cells(4), DataControlFieldCell).ContainingField, CommandField).ShowDeleteButton = True
End If
End If
End Sub
Thank in advance
|
|
|
|
|
You don't need to worry about the header row, since the ItemTemplate doesn't apply there.
So that just leaves the Else block, which is toggling the button's visibility based on a condition. You can do the same thing with a data-binding expression.
Something like this should work:
<asp:Button ID="sourceDelete" runat="server"
...
Visible='<%# Convert.ToInt32(ViewState("rowIndex" & 0)) = Container.DisplayIndex %>'
/>
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi Richard,
You always come up with the simplest, easy to understand solutions.
Thank you very much.
As I understand, I need to trash the VB portion of the code and just use the Visible='<#...))
If so, the delete button is still visible.
It needs to be invisible until the Add button is clicked.
|
|
|
|
|
How can I create an organisational chart and be able to click it and see information on it?
|
|
|
|
|
I suggest you look a Visio for drawing the organizational chart. Then consider using VBA which can be invoked on a shape to get more detailed information.
Visio VBA reference
Good luck
|
|
|
|
|
HI,
i want to return token if user is valid but i do not want to use identity to generate token. so please guide me how to achieve it or redirect me to any relevant article which show me how to generate token without identity or membership etc.
thanks
|
|
|
|
|
Are you authenticating completely out of band with ASP.NET? I'm trying to figure out how you would apply privileges in that scenario, and failing.
"There are three kinds of lies: lies, damned lies and statistics."
- Benjamin Disraeli
|
|
|
|
|
i asked this question out of my curiosity. i like to know is it possible or is it mandatory that some one need to use identity because i am working with old version of asp.net mvc project then how can i generate token where identity does not exist.
thanks
|
|
|
|
|
The .NET ecosystem comprises of 3 major high-level components, namely .NET Framework, .NET Core, and Xamarin.
The .NET Framework is used to create Windows applications using WPF and Windows Forms and Web applications making use of ASP.NET MVC.
How .NET Core fits in the .NET Ecosystem?
|
|
|
|
|
pls send add to cart code in asp.net using sql server
|
|
|
|
|
No. If you want some code written then you need to write it. Alternatively you could use Google or Bing and do some searching for yourself.
|
|
|
|
|
Gladly. Send your credit card information first though.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
By the way, who is Ashraf and why did you call their name?
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
I have developed a tabular UI with webgrid. i am showing student information through webgrid. i am showing multiple checkboxes for hobbies in each row of webgrid. when i select hobbies and click submit button then i saw hobbies selection is not going to action.
i guess there is my mistake in view model class design. please have a look at my code and tell me which area i need to change in code.
i want all hobbies should go to action when i click submit button and selected hobbies also should post to action for each student. a student may have multiple hobbies selected.
here is my viewcode
@model MVCCRUDPageList.Models.StudentListViewModel
@{
ViewBag.Title = "Index";
}
<h2>Student View Model</h2>
@using (Html.BeginForm("Index", "WebGridMoreControls", FormMethod.Post))
{
var grid = new WebGrid(Model.Students, canSort: false, canPage: false);
var rowNum = 0;
var SelectedHobbies = 0;
<div id="gridContent" style=" padding:20px; ">
@grid.GetHtml(
tableStyle: "table",
alternatingRowStyle: "alternate",
selectedRowStyle: "selected",
headerStyle: "header",
columns: grid.Columns
(
grid.Column(null, header: "Row No", format: item => rowNum = rowNum + 1),
grid.Column("ID", format: (item) => @Html.TextBoxFor(m => m.Students[rowNum - 1].ID, new { @class = "edit-mode" })),
grid.Column("Name", format: (item) => @Html.TextBoxFor(m => m.Students[rowNum - 1].Name, new { @class = "edit-mode" })),
grid.Column("Country", format: (item) =>
@Html.DropDownListFor(x => x.Students[rowNum - 1].CountryID,
new SelectList(Model.Country, "ID", "Name", item.CountryID),
"-- Select Countries--", new { id = "cboCountry", @class = "edit-mode" })),
grid.Column(header: "Hobbies",
format: @<text>
Hobbies
@foreach (var hobby in Model.Hobbies)
{
<div class="checkbox">
<label>
@Html.HiddenFor(e => e.Hobbies)
<input type="checkbox"
name="Hobbies"
value="@hobby.ID" /> @hobby.Name
</label>
</div>
}
</text>)
))
<input type="submit" value="Submit" />
</div>
}
Action code
public class WebGridMoreControlsController : Controller
{
// GET: WebGridMoreControls
public ActionResult Index()
{
StudentListViewModel osvm = new StudentListViewModel();
return View(osvm);
}
[HttpPost]
public ActionResult Index(StudentListViewModel oStudentListViewModel)
{
return View(oStudentListViewModel);
}
}
View model code
public class StudentListViewModel
{
public IList<Student> Students { get; set; }
public List<Country> Country { get; set; }
public IList<Hobby> SelectedHobbies { get; set; }
public IList<Hobby> Hobbies { get; set; }
public StudentListViewModel()
{
Students = new List<Student>
{
new Student{ID=1,Name="Keith",CountryID=0,Hobby=0},
new Student{ID=2,Name="Paul",CountryID=2,Hobby=0},
new Student{ID=3,Name="Sam",CountryID=3,Hobby=0}
};
Country = new List<Country>
{
new Country{ID=1,Name="India"},
new Country{ID=2,Name="UK"},
new Country{ID=3,Name="USA"}
};
Hobbies = new List<Hobby>
{
new Hobby{ID=1,Name="Football"},
new Hobby{ID=2,Name="Hocky"},
new Hobby{ID=3,Name="Cricket"}
};
}
}
Model code
<pre lang="c#"> public class Student
{
public int ID { get; set; }
[Required(ErrorMessage = "First Name Required")]
public string Name { get; set; }
public int CountryID { get; set; }
public int Hobby { get; set; }
}
public class Country
{
public int ID { get; set; }
public string Name { get; set; }
}
public class Hobby
{
public int ID { get; set; }
public string Name { get; set; }
}
please help me to rectify view, viewmodel and model class code. thanks
|
|
|
|
|
Here is how i read the json file.
[System.Web.Services.WebMethod]
public static string read()
{
object obj = JsonConvert.DeserializeObject<string>(File.ReadAllText(@"C:\Users\User\Documents\Visual Studio 2010\Projects\WebApplication1\WebApplication1\test\dataTbl.json"));
return obj.ToString();
}
but i getting an error: Error reading string. Unexpected token: StartArray. Path '', line 1, position 1.
Anyone can help?
modified 27-Feb-18 2:48am.
|
|
|
|
|
I think your JSON data needs to start and end with curly braces, not square brackets. So it should be something like:
{
[
{
"name": "chris",
"ic": "111111-11-1111",
"phone": "011-1111111",
"address": "street 01",
"gender": "male"
},
{
"name": "carolyn",
"ic": "222222-22-2222",
"phone": "012-2222222",
"address": "street 02",
"gender": "female"
}
]
}
And you may also need a label in front of the main array to identify it.
|
|
|
|
|
If you look at the documentation[^], or google for any example on-line, you'll see the type you supply DeserializeObject is the type you want the json to be converted to. You are saying you want the json converted to a string which makes no sense as it is already a string. So you're confusing the json parser.
Again referring to the documentation you need to create a class that represents your data and deserialise to that. So create a class like
public class Person
{
public string name { get; set; }
public string ic { get; set; }
public string address { get; set; }
public string gender { get; set; }
}
as your json is an array of these objects you was to convert it to a List<Person>
List<Person> obj = JsonConvert.DeserializeObject<List<Person>>( ... );
|
|
|
|
|
i want to update a local json file (add & remove) at server side, can anyone show me how to do it?
I pass a id parameter to determine which record i want to remove from client side to server side using ajax
$.ajax({
type: "POST",
url: "WebForm1.aspx/remove",
data: "{id:" + id+ "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
after that i had no idea how to code to modify the local file.
[System.Web.Services.WebMethod]
public static void remove(string id)
{
}
|
|
|
|
|
You can't modify files on the client from your server code.
|
|
|
|
|
How to prevent "OnSelectedIndexChanged" event triggered in DropDownCheckBoxes. When DropDownCheckBoxes Bound using asp.net?
|
|
|
|
|
How to enable Searchable dropdowncheckboxes (like AutoFilter) in asp.net
|
|
|
|
|
How to Filter the data in GridView using Linq Query without going to DataBase in VB.NET
|
|
|
|
|
|
i am not sure that always browser cache pages we visited. so my question is when browser cache web site pages? whatever site we visit browser cache each pages ?
if yes then how long browser cache each pages ?
how browser check website pages changes compared with client side cache pages ?
please help me with knowledge. thanks
|
|
|
|
|
people often load js file from CDN. what is advantage?
people said js file is cached there in CDN so load faster. so what can i do to cache js file where my site is deployed?
thanks
|
|
|
|
|