티스토리 뷰
ViewState에 저장되어
해킹
없이 클라이언트에서 액세스 할 수 없습니다 .-------------------
마침내 제가 찾고 있던 답을 얻었습니다!asp.net CheckboxList 컨트롤
은
실제로 value 속성을 HTML로 렌더링합니다. 현재 프로덕션 사이트에서 1 년 넘게 작동하고 있습니다! (우리는 항상 모든 사이트에 대해 EnableViewState를 끄고 조정이나 해킹 없이도 여전히 작동합니다)그러나 갑자기 작동이 중단되고 CheckboxList 확인란이 더 이상 값 속성을 HTML로 렌더링하지 않았습니다! 뭐라고? 우리도 그래! 이 문제를 알아내는 데는 시간이 좀 걸렸지 만 이전에 작동 했으므로 이유가 있어야한다는 것을 알았습니다. 그 이유는 web.config를 실수로 변경했기 때문입니다!
<pages controlRenderingCompatibilityVersion="3.5">
페이지 구성 섹션에서이 속성을 제거했고 그게 트릭이었습니다!왜 그렇습니까? CheckboxList 컨트롤에 대한 코드를 반영하고 RenderItem 메서드에서이를 발견했습니다.
if (this.RenderingCompatibility >= VersionUtil.Framework40)
{
this._controlToRepeat.InputAttributes.Add("value", item.Value);
}
사랑하는 개발 형제 자매 여러분, 절망하지 마세요! Stackexchange에서 여기에서 검색 한 모든 답변과 나머지 웹은 잘못된 정보를 제공했습니다! .Net 4.0부터 asp.net은 CheckboxList의 확인란 값 속성을 렌더링합니다.
<input type="checkbox" value="myvalue1" id="someid" />
실질적으로 유용하지 않을 수도 있습니다. Microsoft는 web.config에 "controlRenderingCompatibilityVersion"을 추가하여 4보다 낮은 버전으로 설정하여이 기능을 해제 할 수있는 기능을 제공했습니다.이 기능은 자바 스크립트 코드에 의존하기 때문에 우리의 목적 상 완전히 불필요하고 실제로 해 롭습니다. 값 속성 ...모든 체크 박스에 대해 "on"과 동일한 chk.val ()을 얻었습니다. 이것은 처음에 체크 박스의 값을 가져 오는 jquery.val ()을 사용하여 처음에이 문제에 대해 경고 한 것입니다. "는 체크 박스의 값입니다 ... 매일 배우십시오).-------------------
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBoxList ID="CheckBoxList1" runat="server" DataTextField="tx" DataValueField="vl">
</asp:CheckBoxList>
</div>
<input id="Button1" type="button" value="button" onclick="return Button1_onclick()" />
</form>
</body>
<script type="text/javascript">
function Button1_onclick()
{
var itemarr = document.getElementById("CheckBoxList1").getElementsByTagName("span");
var itemlen = itemarr.length;
for(i = 0; i <itemlen;i++)
{
alert(itemarr[i].getAttribute("dvalue"));
}
return false;
}
</script>
암호
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("tx");
dt.Columns.Add("vl");
DataRow dr = dt.NewRow();
dr[0] = "asdas";
dr[1] = "1";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "456456";
dr[1] = "2";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "yjryut";
dr[1] = "3";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "yjrfdgdfgyut";
dr[1] = "3";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "34534";
dr[1] = "3";
dt.Rows.Add(dr);
CheckBoxList1.DataSource = dt;
CheckBoxList1.DataBind();
foreach (ListItem li in CheckBoxList1.Items)
{
li.Attributes.Add("dvalue", li.Value);
}
}
}
-------------------
확인란 목록을 해킹하지 않으려면 다음과 같이 중계기를 사용하십시오.
<asp:Repeater ID="rptItems" runat="server" DataSourceID="odsDataSource">
<ItemTemplate>
<input id="iptCheckBox" type="checkbox" runat="server" value='<%# Eval("Key") %>'><%# Eval("Value") %></input>
</ItemTemplate>
</asp:Repeater>
-------------------
이 작업을 수행하려면 정말 불쾌한 일을해야했습니다 ..
<asp:Repeater ID="rptItems" runat="server">
<ItemTemplate>
<input ID="iptCheckBox" type="checkbox" runat="server" value='<%# Eval("your_data_value") %>' />
<label ID="iptLabel" runat="server"><%# Eval("your_data_field") %></label>
<br />
</ItemTemplate>
</asp:Repeater>
코드 숨김 :
Private Sub rptItems_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptItems.ItemDataBound
Dim checkBox As HtmlInputCheckBox = DirectCast(e.Item.FindControl("iptCheckBox"), HtmlInputCheckBox)
Dim label As HtmlControl = DirectCast(e.Item.FindControl("iptLabel"), HtmlControl)
label.Attributes.Add("for", checkBox.ClientID)
End Sub
-------------------
Swapna, 귀하의 답변은 절대적으로 작동합니다. 따라서 ASP.Net Checkboxlist의 확인란이 선택되어 있는지 확인한 다음 목록을 쉼표로 구분 된 문자열로 누적하려면 다음과 같이 할 수 있습니다.C # 코드 숨김
ChkboxList1.DataSource = dsData;
ChkboxList1.DataTextField = "your-display-column-name";
ChkboxList1.DataValueField = "your-identifier-column-name";
ChkboxList1.DataBind();
foreach (ListItem li in ChkboxList1.Items)
{
li.Attributes.Add("DataValue", li.Value);
}
그런 다음 Javascript에서
var selValues = "";
var ChkboxList1Ctl = document.getElementById("ChkboxList1");
var ChkboxList1Arr = null;
var ChkboxList1Attr= null;
if (ChkboxList1Ctl != null)
{
ChkboxList1Arr = ChkboxList1Ctl.getElementsByTagName("INPUT");
ChkboxList1Attr = ChkboxList1Ctl.getElementByTagName("span");
}
if (ChkboxList1Arr != null)
{
for (var i = 0; i < ChkboxList1Arr.length; i++)
{
if (ChkboxList1Arr[i].checked)
selValues += ChkboxList1Attr[i].getAttribute("DataValue") + ",";
}
if (selValues.length > 0)
selValues = selValues.substr(0, selValues.length - 1);
}
-------------------
제거하면 작업
<pages controlRenderingCompatibilityVersion="3.5">
중이던 일부 이전 코드에서 문제가 발생했습니다. 내가지고 있다고
이 오류를
. 오류가 발생하면 변경이 너무 위험하다는 것을 깨달았습니다.이 문제를 해결하기 위해 각 목록 항목에 값이 포함 된 추가 속성을 추가하는 메서드를 확장
CheckBoxList
하고 재정의했습니다
Render
.
public class CheckBoxListExt : CheckBoxList
{
protected override void Render(HtmlTextWriter writer)
{
foreach (ListItem item in this.Items)
{
item.Attributes.Add("data-value", item.Value);
}
base.Render(writer);
}
}
그러면
data-value
외부
span
태그 의 속성 이 렌더링됩니다 .-------------------
Easy Way I Do It.
//First create query directly From Database that contains hidden field code format (i do it in stored procedure)
SELECT Id,Name + '<input id="hf_Id" name="hf_Id" value="'+convert(nvarchar(100),Id)+'" type="hidden">' as Name FROM User
//Then bind checkbox list as normally(i bind it from dataview).
cbl.DataSource = dv;
cbl.DataTextField = "Name";
cbl.DataValueField = "Id";
cbl.DataBind();
//Then finally it represent code as follow.
<table id="cbl_Position" border="0">
<tbody>
<tr>
<td>
<input id="cbl_0" name="cbl$0" type="checkbox">
<label for="cbl_0">
ABC
<input id="hf_Id" name="hf_Id" value="1" type="hidden">
</label>
</td>
</tr>
</table>
This way you can get DataValueField as inside hiddenfield and also can get it value from client side using javascript.
-------------------
나는 다음과 그 일을 시도했다.
<asp:CheckBoxList ID="chkAttachments" runat="server"></asp:CheckBoxList>
서버 측 바인딩 :
private void LoadCheckBoxData()
{
var docList = new List<Documents>(); // need to get the list data from database
chkAttachments.Items.Clear();
ListItem item = new ListItem();
foreach (var doc in docList )
{
item = new ListItem();
item.Value = doc.Id.ToString();
item.Text = doc.doc_name;
chkAttachments.Items.Add(item);
}
}
-------------------
나는 전에 이것을 검색했고 당신은 거의 운이 없을 것입니다. 나는 자바 스크립트에 대한 체크 박스 목록 항목이 없다는 것을 본 기억이있어서 그것을 이해하지 못합니다. 확인란 유형이있는 페이지의 항목을 검색하고 해당 항목이 속한 그룹 (속성 인 것 같음)을 테스트해야합니다.내 코드를 검색하고 내가 한 일을 찾을 수 있는지 확인합니다.
물론 내가 한 일을 찾을 수 없습니다.왜 자바 스크립트에서 클라이언트 측을하고 있습니까? AJAX를 사용하고 모든 것을 서버 측에서 제어하지 않는 이유는 무엇입니까?
출처
https://stackoverflow.com/questions/180245